My Solution with no stack(beats 95.5%)


  • 0
    Z
    public class Solution {
        public String simplifyPath(String path) {
            StringBuilder ret = new StringBuilder();
            String[] paths = path.split("/");
            int curIndex = 0;
            for (String t: paths) {
                if (t.equals("..")) {
                    if (ret.length() != 0) 
                        ret.setLength(ret.lastIndexOf("/"));
                } else if (!t.equals(".") && !t.equals("")) {
                    ret.append("/");
                    ret.append(t);
                }
            }
            String rs = ret.toString();
            return rs.equals("") ? "/" : rs;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.