美文网首页
71. Simplify Path

71. Simplify Path

作者: jecyhw | 来源:发表于2019-05-31 06:20 被阅读0次

题目链接

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

      本文链接:https://www.haomeiwen.com/subject/iceitctx.html