0
点赞
收藏
分享

微信扫一扫

【LeetCode】环形队列实现

郝春妮 2024-05-07 阅读 7

题目:

题解:

class Solution {
    public String simplifyPath(String path) {
        String[] names = path.split("/");
        Deque<String> stack = new ArrayDeque<String>();
        for (String name : names) {
            if ("..".equals(name)) {
                if (!stack.isEmpty()) {
                    stack.pollLast();
                }
            } else if (name.length() > 0 && !".".equals(name)) {
                stack.offerLast(name);
            }
        }
        StringBuffer ans = new StringBuffer();
        if (stack.isEmpty()) {
            ans.append('/');
        } else {
            while (!stack.isEmpty()) {
                ans.append('/');
                ans.append(stack.pollFirst());
            }
        }
        return ans.toString();
    }
}
举报

相关推荐

Java实现环形队列

Java数组实现队列+环形队列

c语言环形队列

4.环形队列

Leetcode—环形链表||

leetcode:环形链表

LeetCode环形链表Ⅱ

0 条评论