lamiastella

modified path simplify

Jun 2nd, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class Solution:
  2.     # @param A : string
  3.     # @return a strings
  4.     #path = "/home/", => "/home"
  5.     #path = "/a/./b/../../c/", => "/c"
  6.     def simplifyPath(self, A):
  7.         stack = []
  8.         result = ''
  9.         for char in A:
  10.             stack.append(char)
  11.         if A[len(A)-1] == '/':
  12.             last_char_slash = True
  13.         else:
  14.             last_char_slash = False
  15.         while True:
  16.             if len(stack) == 0:
  17.                 return result[::-1]
  18.             tmp = stack.pop()
  19.             if tmp == '/' and last_char_slash == True:
  20.                 continue
  21.             if tmp == '/':
  22.                 result += tmp
  23.                 return result[::-1]
  24.             if tmp != '/':
  25.                 result += tmp
  26.  
  27.  
  28. s = Solution()
  29. print(s.simplifyPath("/a/./b/../../c/"))
Advertisement
Add Comment
Please, Sign In to add comment