problem:https://leetcode.com/problems/mini-parser/git
一道普通的翻譯題?代碼寫的很不優雅:spa
class Solution { public: NestedInteger deserialize(string s) { if(s.size() && s.front() != '[' && s.back() != ']') { return NestedInteger(atoi(s.c_str())); } s = s.substr(1, s.size() - 2); NestedInteger res; int count = 0; string strValue; string strList; for(int i = 0;i < s.size(); i++) { if(s[i] == '[') { count++; } else if(s[i] == ']') { count--; if(count == 0) { strList.push_back(s[i]); res.add(deserialize(strList)); strList.clear(); } } if(count == 0) { if(isdigit(s[i]) || s[i] == '-') { strValue.push_back(s[i]); } else if(s[i] == ',' && !strValue.empty()) { res.add(NestedInteger(atoi(strValue.c_str()))); strValue.clear(); } } else if(count >= 1) { strList.push_back(s[i]); } } if(!strValue.empty()) res.add(NestedInteger(atoi(strValue.c_str()))); return res; } };