Given an absolute path for a file (Unix-style), simplify it.
For example,
Path = "/home/" , = ="/home"
Path = "/a/./b/../../c/" , = ="/c"
Path = "/a/./b/../c/" , = ="/a/c"
path = "/a/./b/c/", => "/a/b/c"
Click to show corner cases.
Corner Cases:
- 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 stringSimplifypath (stringpath) {List<string> list =Newlist<string>(); string[] Words = path. Split ('/'); for(inti =0; i< words. Count (); i++) { if(words[i]==""|| Words[i] = =".") { ; } Else if(Words[i] = ="..") { if(list. Count () >0) List. RemoveAt (list. Count ()-1); } Elselist. ADD (Words[i]); } stringres =""; foreach(varLinchlist) {Res+="/"+l; } if(res = ="")return "/"; returnRes; }
Simplify Path questioneditorial Solution