您的位置:首页 > 职场人生

[LeetCode] Count and Say

2013-01-28 04:38 351 查看
class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> v;
vector<int> t;

v.push_back(1);

for (int i = 1; i < n; i++) {
t.clear();

for (int pos = 0; pos < v.size(); pos++) {
int count = 1;

while (pos + 1 < v.size() && v[pos] == v[pos + 1]) {
count++;
pos++;
}

t.push_back(count);
t.push_back(v[pos]);
}

v = t;
}

string ret = "";

for (int i = 0; i < v.size(); i++)
ret += v[i] + '0';

return ret;
}
};


Small Case: 8ms

Large Case: 8ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息