Source of the topic:
https://leetcode.com/problems/simplify-path/
Test Instructions Analysis:
simplifies the absolute path on UNIX, which means that multiple '/' stands for one, ' ... ' Returns the previous level of the directory, '. ' Represents the current directory.
Topic Ideas:
The use of stacks, the non-'/' and '. ' Push into the stack if you encounter '.. ' Pop off one, or push it in again. Finally revert to the path format.
Code (Python):
classsolution (object):defSimplifypath (self, path):""": Type Path:str:rtype:str"""Stack,i,ans= [],0,"' whileI <len (path): J= i + 1 whileJ < Len (path) andPATH[J]! ='/': J+ = 1tmp= Path[i + 1: j]ifTMP! ="': ifTMP = ='..': ifStack! =[]: Stack.pop ()elifTMP! ='.': Stack.append (tmp) I=Jifstack = = []: return '/' forKinchStack:ans+='/'+kreturnAns
View Code
Reprint Please specify source: http://www.cnblogs.com/chruny/p/5069660.html
[Leetcode] (python): 071-simplify Path