71. Simplify Path

题目链接

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

解题思路

直接看代码

代码

class Solution {
    public String simplifyPath(String path) {
        String[] paths = path.split("/");
        int len = 0;
        for (String s : paths) {
            if (s.equals("..")) {
                if (len > 0) {
                    len--;
                }
            } else if (!s.equals(".") && s.length() > 0) {
                paths[len++] = s;
            }
        }
        if (len == 0) {
            return "/";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append("/");
            sb.append(paths[i]);
        }
        return sb.toString();
    }
}

你可能感兴趣的:(71. Simplify Path)