71. 简化路径

题目:

https://leetcode-cn.com/problems/simplify-path/

import java.util.ArrayDeque;
import java.util.Deque;

public class _71_SimplifyPath {
    public String simplifyPath(String path) {
        Deque<String> stack = new ArrayDeque<>();
        String[] paths = path.split("/");
        for (int i = 0; i < paths.length; i++) {
            String name = paths[i];
            if (name.equals("")) {
                continue;
            } else if (name.equals(".")) {
                continue;
            } else if (name.equals("..")) {
                if (!stack.isEmpty()) {
                    stack.pop();
                }
            } else {
                stack.push(name);
            }
        }
        String ans = "";
        int size = stack.size();
        for (int i = 0; i < size; i++) {
            ans += "/";
            ans += stack.pollLast();
        }
        if (ans.equals("")) {
            ans += "/";
        }
        return ans;
    }
}
上一篇:71. VUE axios 发送并发请求(多个)


下一篇:2019 上半年信息系统项目管理师上午综合知识真题(71)