首页 > 代码库 > LeetCode Count and Say
LeetCode Count and Say
class Solution {public: string countAndSay(int n) { vector<int> num; num2digit(1, num); vector<int> tmp; for (int i = 1; i<n; i++) { int last = num[0]; int cur = last; int count= 1; for (int i=1; i < num.size(); i++) { cur = num[i]; if (cur != last) { num2digit(count, tmp); tmp.push_back(last); last = cur; count = 1; } else { count++; } } num2digit(count, tmp); tmp.push_back(cur); swap(num, tmp); tmp.clear(); } string res; for (int i=0; i<num.size(); i++) { res.push_back((char)(num[i] + ‘0‘)); } return res; } void num2digit(int n, vector<int> &digits) { vector<int> res; if (n < 0) { n = -n; } do { res.push_back(n % 10); n /= 10; } while (n != 0); for (int i=res.size() - 1; i >= 0; i--) { digits.push_back(res[i]); } }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。