[LeetCode] Count and Say

简介: This problem is purely to test your coding ability. Just go ahead :-) 1 class Solution { 2 public: 3 string countAndSay(int n) { 4 ...

This problem is purely to test your coding ability. Just go ahead :-)

 1 class Solution {
 2 public:
 3     string countAndSay(int n) {
 4         string num = "1";
 5         for (int i = 1; i < n; i++)
 6             num = say(num);
 7         return num;
 8     } 
 9 private:
10     string say(string& num) {
11         string s;
12         int n = num.length(), i = 0, j, k;
13         for (int i = 0; i < n; i = j, k = 0) {
14             k = 1, j = i + 1;
15             while (j < n && num[j] == num[i]) j++, k++;
16             s += char(k + '0');
17             s += num[i];
18         }
19         return s;
20     }
21 };

 

目录
相关文章
LeetCode 357. Count Numbers with Unique Digits
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。
63 0
LeetCode 357. Count Numbers with Unique Digits
LeetCode 222. Count Complete Tree Nodes
给出一个完全二叉树,求出该树的节点个数。
65 0
LeetCode 222. Count Complete Tree Nodes
LeetCode之Count and Say
LeetCode之Count and Say
80 0

热门文章

最新文章