码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Simplify Path

时间:2015-12-06 09:55:06      阅读:158      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!