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 ...
随机推荐
- 黑马程序员_JAVA之银行业务调度系统
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 1.模拟实现银行业务调度系统逻辑,具体需求如下: 银行内有6个业务窗口,1 - 4号窗口为普通窗 ...
- GLSL语言基础
from http://www.kankanews.com/ICkengine/archives/120870.shtml 变量 GLSL的变量命名方式与C语言类似.变量的名称可以使用字母,数字以及下 ...
- net.sf.json 时间格式的转化
后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...
- PCI Express(三) - A story of packets, stack and network
原文出处:http://www.fpga4fun.com/PCI-Express3.html Packetized transactions PCI express is a serial bus. ...
- mvc多个按钮的提交方法
转载地址:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html 有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能, ...
- 搭建nexus后,进入首页的时候出现warning: Could not connect to Nexus.错误
nexus出现这种问题,一般是版本太旧,换一个高版本的nexus就能解决了.
- tcl使用笔记
tcl语法网站:http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm 1)拷贝文件 set PRJ_HDL_DIR "../prj/hdl&quo ...
- win10开始菜单打不开怎么办 win菜单键没反应解决办法
win10开始菜单打不开怎么办 win菜单键没反应解决办法 —————————————————————————————————————————————————————————————————————— ...
- gcc相关
linux操作系统上面开发程序, 光有了gcc 是不行的 它还需要一个 build-essential软件包作用是提供编译程序必须软件包的列表信息 也就是说 编译程序有了这个软件包它才知道 头文件 ...
- VS2008中调试dll
1.运行dll实例时,会直接弹出一个小框: 选择可拉起这个dll的exe运行就可以调试了 2.以后每次都会直接运行了,要重新选择程序,弹出上面的框,需要在project-->debugging- ...