首页 > 其他 > 详细

[LeetCode]Simplify Path

时间:2015-12-06 09:54:15      阅读:263      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public String simplifyPath(String path) {
        String[] strs = path.split("/");
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].equals("..")) {
                if (!stack.isEmpty())
                    stack.pop();
            } else if (strs[i].equals(".") || strs[i].length() == 0) {
                continue;
            } else {
                stack.push(strs[i]);
            }
        }
        StringBuffer r_b = new StringBuffer("");
        while (!stack.isEmpty()) {
            r_b.insert(0, "/" + stack.pop());
        }
        String result = String.valueOf(r_b);
        if (result.length() == 0) {
            return "/";
        }
        return result;
    }
}

 

[LeetCode]Simplify Path

原文:http://www.cnblogs.com/vision-love-programming/p/5022871.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!