[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 。
222 0
LeetCode 357. Count Numbers with Unique Digits
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
204 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 222. Count Complete Tree Nodes
给出一个完全二叉树,求出该树的节点个数。
141 0
LeetCode 222. Count Complete Tree Nodes
|
测试技术
LeetCode 204. Count Primes
统计所有小于非负整数 n 的质数的数量。
176 0
LeetCode 204. Count Primes
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode之Count and Say
LeetCode之Count and Say
143 0
LeetCode 204 Count Primes(质数计数)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50617645 翻译 计算小于一个非负整数n的质数的个数。
927 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
254 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行

热门文章

最新文章