388. Longest Absolute File Path
Description
Solution
Recollection the use of stringstream
:
http://www.cplusplus.com/reference/sstream/stringstream/
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class Solution { public: int lengthLongestPath(string input) { stringstream ss(input); string token; vector<string> paths; int res = 0; while(getline(ss, token, '\n')){ int layer = 0; int length = 0; for(int i = 0; i < token.size(); i++){ if(token[i] != '\t') break; layer++; } token = token.substr(layer); if(isFile(token)){ for(int i = 0; i < layer; i++) length += paths[i].size() + 1; res = max(length + (int)token.size() , res); }else{ if(paths.size() >= layer+1) paths[layer] = token; else paths.push_back(token); } } return res; }
bool isFile(string& token){ for(auto c : token) if(c == '.') return true; return false; }
};
|