421. Simplify Path

string simplifyPath(string &path) { // write your code here const int size = path.size(); if(size == 0){ return "/"; } std::vector<string> strs; int i = 0; while(i < size){ while(i < size && path[i] == '/'){ i++; } if(i == size){ break; } int start = i; while(i < size && path[i] != '/'){ i++; } string sub = path.substr(start, i - start); if(sub == "."){ continue; } else if(sub == ".."){ if(!strs.empty()){ strs.pop_back(); } } else{ strs.push_back(sub); } } if(strs.size() == 0){ return "/"; } string res = ""; for(int i = 0; i < strs.size(); i++){ res += "/" + strs[i]; } return res; }

Comments

Popular posts from this blog

1427. Split Array into Fibonacci Sequence

Amazon OA 763. Partition Labels

05/25 周一