Simplify Path

来源:互联网 发布:ccer金融数据库 编辑:程序博客网 时间:2024/05/22 14:19
public class Solution {    public String simplifyPath(String path) {if (path == null || path.length() == 0) {    return "";}Stack<String> stack = new Stack<>();String[] strs = path.split("/");for (String str: strs) {    if (!str.equals("") && !str.equals(".")) {        if (str.equals("..")) {            if (!stack.isEmpty()) {                stack.pop();            }        } else {            stack.push(str);        }    }}StringBuilder sb = new StringBuilder();while (!stack.isEmpty()) {    sb.insert(0, stack.pop());    sb.insert(0, "/");}if (sb.length() == 0) {    return "/";} else {    return sb.toString();}    }}

0 0
原创粉丝点击