LeetCode 之 Count and Say
来源:互联网 发布:网络零售商有哪些 编辑:程序博客网 时间:2024/04/25 12:33
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.
这个题的描述有点迷惑性,而且这还是有过更正的,我看discuss上有人讨论说之前更误导人。只看前五个例子,还是有可能理解错,正确的第6的sequence:312211。现在我们理解这个题的意思了,“个数”+“数字”组合起来,实现上从低到高迭代生成就行了。
<span style="color:#333333;">class Solution {public: string countAndSay(int n) { string ans="1"; if(n==1) return ans; //string ans=last; for(int i=2;i<=n;i++){ string temp; int m=0,n=0; while(n!=ans.size()){ if(ans[n]==ans[m]&&n==(ans.size()-1)){</span><span style="color:#cc0000;">temp+=std::to_string(n-m+1)+ans[m]</span><span style="color:#333333;">;n++;} else if(ans[n]==ans[m]&&n!=(ans.size()-1)){ n++; }else{ </span><span style="color:#ff0000;">temp+=std::to_string(n-m)+ans[m];</span><span style="color:#333333;"> m=n; } } ans=temp; } return ans; }};</span>
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之路038 Count and Say
- LeetCode之count-and-say sequence
- 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
- 作业
- 打桩法与普通debug工具的优劣及使用打桩法的技巧
- 清除所有的mobileprovision
- Java学习之Iterator(迭代器)的一般用法 (转)
- 用多线程并发的方式来计算两个矩阵的乘法
- LeetCode 之 Count and Say
- Andorid获取状态栏高度的三种方法
- RS485基本概念及可靠性设计
- 弄懂Android手机、pad适配的dp与px
- 玩转云端(云服务器使用详解)
- javascript模板引擎——artTemplate
- FLEX 网格布局及响应式处理
- jquery.validate ie8 验证提交问题
- 表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。