beat 94% java solutions


  • 0
    H

    My solution just use a StringBuilder.
    ...
    public class Solution {
    public String simplifyPath(String path) {
    StringBuilder ans = new StringBuilder("");
    String[] arr = path.split("/");
    if(arr.length == 0) return "/";
    for(String s : arr){
    if(s.equals("..")){
    int t = ans.lastIndexOf("/");
    if(t >= 0){
    ans = ans.delete(t, ans.length());
    }
    }else if(s.equals(".") || s.equals("")){
    //do nothing
    }else{
    ans.append("/").append(s);
    }
    }
    String s = ans.toString();
    return s.length() > 0 ? s : "/";
    }
    }...


Log in to reply
 

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