Count and Say
来源:互联网 发布:未再网络上注册怎么办 编辑:程序博客网 时间:2024/06/05 18:11
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.
class Solution {public: string countAndSay(int n) { if (n < 1) { return ""; } if (n == 1) { return "1"; } std::string ret("1"); for (int i = 2; i <= n; i++) { // we don't need another variable to record // previous resulted string, as we have it // as ret (previous result) and stringstream object // (current result) std::stringstream newStrSeq; char last = ret[0]; int count = 0; // ret[ret.size()] == '\0' for (int j = 0; j <= ret.length(); j++) { if (ret[j] == last) { count++; } else { newStrSeq << count << last; last = ret[j]; count = 1; } } ret = newStrSeq.str(); } return ret; }};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- Count and Say
- [38]Count and Say
- Count and Say
- Count and Say
- Count and Say
- [LeetCode]Count and Say
- Count and Say
- Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- HDU3555 Bomb 数位DP
- 编写Shell脚本监测服务器状态
- 动态规划走楼梯
- 突击战【JAVA算法实现】
- Codeforces Round #277.5 (Div. 2)-C
- Count and Say
- linux 下关闭启动sendmail 服务
- android ListView 一些重要属性详解
- C# 实现设置系统环境变量设置
- git 命令
- 深入解析Apache Mina源码(4)——Mina编解码以及对粘包和断包的处理
- 青岛泽雨你到底怎么了???人去楼空了,难道真的要倒闭了吗?
- Codeforces Round #277.5 (Div. 2)-B
- 初级会计实务试题要怎么复习