Count and Say leetcode
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.
题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。
http://blog.csdn.net/kenden23/article/details/17081853
https://github.com/krystism/leetcode/tree/master/algorithms/CountandSay
http://www.cnblogs.com/huxiao-tee/p/4109596.html
注意:用n(int)+'0'表示'n',仅当n<10时有效!
class Solution {
public:
string countAndSay(int n) {
if(==n)
return NULL;
string result="";
while(--n){
result=nextSequence(result);
}
return result;
}
private:
string nextSequence(string s1){
char cur=s1[];
int count=;
string result;
for(int i=;i<s1.size();i++){
if(cur!=s1[i]){
result+=itos(count);
result.push_back(cur);
cur=s1[i];
count=;
}
else{
count++;
}
}
result+=itos(count);
result.push_back(cur);
return result;
}
string itos(int i){
ss.str("");
ss.clear();
ss<<i;
return ss.str();
}
stringstream ss;
};
Count and Say leetcode的更多相关文章
- Count and Say leetcode java
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- Count and Say [LeetCode 38]
1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode Patching Array
原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...
- leetcode math类型题目解题总结
2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution { publi ...
- LeetCode哈希表
1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
随机推荐
- 使用opencv设置图像的格式以及帧率
最近楼主正在写一个关于图像存储的程序,LZ有一颗求知心,想要了解保存的图像的格式以及获取摄像头帧率.晚些时候会写一篇关于opencv获取摄像头并且保存每帧图像信息方法. 1.修改图像的像素显示: 首先 ...
- C#全角、半角转换
全角:指一个字符占用两个标准字符位置:半角:指一字符占用一个标准的字符位置. using System; using System.Collections.Generic; using System. ...
- 7,SFDC 管理员篇 - 数据模型 - 公式和验证 1
1,自定义公式 Customize | Your Object | Fields | Add Fields Field SF的公式和Excel的公式差不多,都是支持各种运算和结果 例1,以opport ...
- iOS逆向分析app
适合有一定的逆向编程基础的人看. 背景:自动抢红包的脚本工具:cyscript,reveal,class-dump,越狱的pod等. 这里先上一张reveal的分析图: 小结:获取到了真个软件的整体结 ...
- [Jquery] Jquery AutoComplete的使用方法实例
jQuery的Autocomplete(自动完成.自动填充)插件 jquery-autocomplete配置: <script type="text/javascript" ...
- Linux: vi 编辑器中文乱码
[问题] vi编辑器中文乱码 [解决方案] vi /etc/sysconfig/i18n改成如下即可:LANG="en_US.UTF-8"LANGUAGE="zh_CN. ...
- makefile学习小结
=============2016/08/15================ 上午完成makefile的试验,缩短了代码量,现在make强大,有缺省的变量,能自己推导关系,不需要gcc –MM -M ...
- android自定义进度圆与定时任务
先看代码:自定进度圆 public class ProgressCircle extends View { private Paint paint; private int strokewidth = ...
- iOS学习笔记(5)— UITextField
UITextField详解 一.基本属性 1.创建文本输入框 UITextField*textField=[[UITextField alloc]initWithFrame:CGRectMake(10 ...
- C++新特性(类)(转载)
C++新特性(类)里面讲的很清楚,转给大家分享一下 类机制: 类是对某一类对象的抽象:对象是某一类的实例: 类是一种复杂的数据类型,将不同类型的数据和这些数据相关的操作封装在一起的集合体: 通过一道程 ...