71. 简化路径
1 // "/a//b////c/d//././/.." 2 class Solution 3 { 4 vector<string> res; 5 void spilt(string s,char c,vector<string> &res) 6 { 7 istringstream iss(s); 8 string temp; 9 while(getline(iss,temp,c)) 10 { 11 //如果temp不为空,才可以添加进去 12 if(!temp.empty()) res.push_back(temp); 13 } 14 } 15 16 public: 17 string simplifyPath(string path) 18 { 19 string result; 20 spilt(path,'/',res); 21 int n = res.size(); 22 deque<string> q; 23 for(int i = 0;i < n;i ++) 24 { 25 if(res[i] == ".") continue; 26 else if(res[i] == "..") 27 { 28 if(!q.empty()) q.pop_back(); 29 } 30 else q.push_back(res[i]); 31 } 32 if(q.empty()) return "/"; 33 34 while(!q.empty()) 35 { 36 result = result + "/" + q.front(); 37 q.pop_front(); 38 } 39 return result; 40 } 41 };
Mamba never out

浙公网安备 33010602011771号