9ms Java clean solution


  • 0
    A
    public String simplifyPath(String path) {
            String[] dirs = path.split("/");
            int len = 0;
            for (int i = 0; i < dirs.length; i++) {
                String dir = dirs[i];
                if (dir.equals("..") && len > 0) {
                    len--;
                } else if (dir.equals(".") || dir.equals("..") || dir.isEmpty()) {
                    continue;
                } else {
                    dirs[len++] = dir;
                }
            }
            
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < len; i++) {
                sb.append("/").append(dirs[i]);
            }
            
            if (sb.length() == 0) sb.append("/");
            return sb.toString();
        }

Log in to reply
 

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