Given an absolute path for a file (Unix-style), simplify it.
Example
"/home/", ="/home"
"/a/./b/../../c/", ="/c"
Challenge
Did you consider the case where path = "/../" ?
In this case, you should return "/" .
Another corner case is the path might contain multiple slashes ‘/‘ together, such as "/home//foo/" .
Should ignore redundant slashes and return "/home/foo" .
Public classSolution {/** * @parampath The original path *@returnThe simplified path*/ Publicstring Simplifypath (string path) {//Write Your code here if(Path = =NULL|| Path.length () = = 0) returnpath; Stack<String> stack =NewStack<string>(); inti = 0; intj = 1; intLen =path.length (); while(J <Len) { while(J < Len && Path.charat (j)! = '/') J++; String Temp=path.substring (i, j); if(Temp.equals ("/.") | | temp.equals ("/")) ) {i=J; J++; } Else if(Temp.equals ("/.."))){ if(!stack.isempty ()) Stack.pop (); I=J; J++; } Else{Stack.push (temp); I=J; J++; } } if(Stack.isempty ())return"/"; String Res= ""; while(!Stack.isempty ()) {Res= Stack.pop () +Res; } returnRes; }}
Lintcode-medium-simplify Path