Leetcode Count and Say
来源:互联网 发布:mash up软件下载 编辑:程序博客网 时间:2024/06/10 21:32
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
第一次出现的是1,第二次出现的是11,第三次出现的是21,第四次出现的是1211,一次类推,求第N次出现的字符串。主要是N次循环,每次循环找到数字出现的字数,然后保存在string中。用到一个数字转化成字符串函数,sprintf(s,”%d”,124354)
class Solution {public: string countAndSay(int n) { string result = "1"; string str=""; char strcount[100]; if (n==1) return result; for(int i=2; i<=n; i++) { str=""; int len = result.length(); int count = 0; char tmp = result[0]; for(int j=0; j<len; j++) { if (tmp == result[j]) { count++; } else { sprintf(strcount,"%d",count); str = str + strcount + tmp; count = 0; tmp = result[j]; j--; } } sprintf(strcount,"%d",count); str = str + strcount + tmp; result = str; } return str; }};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- Binary Tree Preorder Traversal
- 宁早不晚,美国想要汽车之间彼此对话
- 什么是回文数
- 微软100题(3) 最大子数组和
- [Linux]内存共享
- Leetcode Count and Say
- 使用git
- java中的注释
- 排序汇总
- POJ2391解题报告
- js eval()函数用法
- 完全使用Linux
- shell脚本----cp (copy)复制文件或目录
- 覆铜中的一些规则设置(个人使用)