Count and Say
来源:互联网 发布:小米4能用移动4g网络吗 编辑:程序博客网 时间:2024/06/10 23:00
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是第二个数字,输出第n个数字。
class Solution {public: string countAndSay(int n) { string temp,result; result.push_back('1'); //字符串result后面添加1; int count; char pre; for(int i=1;i<n;i++){ //n为2时,只要进行一次,输出11; temp=result; result.clear(); pre=temp[0]; count=1; for(int j=1;j<temp.size();j++){ if(temp[j]==pre) count++; else{ //temp[j]与之前的pre不相等,更行results,并把pre移到temp[j]处,打印出tem[j]之前的。 result.push_back('0'+count); result.push_back(pre); pre=temp[j]; count=1; } } result.push_back('0'+count);//添上最后一个结束符 result.push_back(pre); } return result; }};
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
- linux下iptraf和netperf的应用
- 百度新接口,让网页秒收
- 4.17&4.18
- list_style li 的点
- ios 直角和圆角 并存
- Count and Say
- NSString什么时候用copy,什么时候用strong
- FrameLayout(框架布局)
- PullToRefreshListView局部刷新
- list_style
- iOS画图 在控件中画图
- CentOS 安装iftop 监控网络流量
- git常用命令
- Longest Consecutive Sequence