38. Count and Say Easy
来源:互联网 发布:vue.js教程 编辑:程序博客网 时间:2024/06/10 04:37
简单题,主要是题目太难理解,没讲清楚,这里转一段别人的解释。
题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推。
代码比较繁琐,懒的改了。。
class Solution {
public:
string countAndSay(int n) {
string s = "1";
return generate(n - 1, s);
}
string generate(int n, string & s)
{
if(n == 0)return s;
char co[20];
char flag = s[0];
int count = 0;
string s2;
for(int i = 0; i < s.size(); i++){
if(s[i] == flag) count++;
else {
sprintf(co, "%d", count);
s2 += co;
s2 += flag;
flag = s[i];
count = 1;
}
}
sprintf(co, "%d", count);
s2 += co;
s2 += flag;
return generate(n - 1, s2);
}
};
0 0
- 38. Count and Say Easy
- Easy-题目50:38. Count and Say
- 38. Count and Say [easy] (Python)
- Leetcode 38. Count and Say (Easy) (cpp)
- Leet Code OJ 38. Count and Say [Difficulty: Easy]
- [Leetcode 38, Easy] Count and Say
- <LeetCode><Easy> 38 Count and Say
- Easy 38题 Count and Say
- Easy 11 Count and Say(38)
- Easy 12 Count and Say(53)
- Count and say[easy on LeetCode]
- (算法分析Week4)Count and Say[Easy]
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- libsvm的使用
- 51nod1003
- 纯CSS3实现不错的表单验证效果
- soy文件的语法规则
- C语言函数传递指针参数的问题
- 38. Count and Say Easy
- 面向对象类之间的引用
- Python基础入门(十三)-切片
- elk
- 一个地道的状态模式列子(java)
- android四大组件---摘选
- Angular2 指令—通用指令
- HTML辨别浏览器版本
- 文章标题