leetcode——38——Count and Say
2016-04-06 20:15
423 查看
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
1, 11, 21, 1211, 111221, ...
1is read off as
"one 1"or
11.
11is read off as
"two 1s"or
21.
21is 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.
class Solution { public: string countAndSay(int n) { string s = "1"; for(int i = 1; i < n; ++ i) { int count = 1; string temp = ""; for(int j = 1; j < s.size(); ++ j) { if(s[j] == s[j - 1]) ++ count; else { temp = temp + (char)(count + '0') + s[j - 1]; count = 1; } } s = temp + (char)(count + '0') + s[s.size() - 1]; } return s; } };
相关文章推荐
- 判断大小端问题
- java nio 之MappedByteBuffer,高效文件/内存映射
- MDK错误提示
- 9 JavaScritp:为数据带来生命-创建自定义对象
- 软件项目管理第五周
- OSGi Manifest元文件
- POJ_2352_Stars
- VCL比MFC好在哪里
- iOS——UIApplication和delegate
- Mybatis Generator 生成的mapper只有insert方法
- 内边距
- Android中的WebView
- 伪代码规范
- 单片机FLASH与RAM、ROM的关系
- 使用@Configuration注解来代替Spring的bean配置
- 第四章项目5-递归求n的阶乘(1)
- c++操作符重载
- 结对开发-四则运算四
- 俄罗斯方块游戏的控制面板和消息接收框界面的实现
- (已翻译)caffe官方例程之hdf5_classification(Brewing Logistic Regression then Going Deeper)