[LeetCode]71 Simplify Path(模拟)
题目链接:https://leetcode.com/problems/simplify-path/?tab=Description
题意:化简一个绝对路径。
1 class Solution { 2 public: 3 string simplifyPath(string path) { 4 string s; 5 int begin = 0, len = 0; 6 for(int i = 1; i <= path.length(); i++){ 7 if(!begin && path[i] != '/') { 8 begin = i; 9 } 10 else if(begin && (path[i] == '/' || i == path.length())) { 11 len = i - begin; 12 } 13 if(len) { 14 string ns = path.substr(begin, len); 15 begin = len = 0; 16 if(!ns.compare(".")) { 17 continue; 18 } 19 else if(!ns.compare("..")) { 20 int j(s.length() - 1); 21 while(s[j] != '/') j--; 22 s = s.substr(0, j); 23 } 24 else s += '/' + ns; 25 } 26 } 27 return s.length() ? s : "/"; 28 } 29 };