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)函数返回字符串。

class Solution {
public:
string convert(const string &say)
{
stringstream ss; //创建一个流
int count=0;
char last=say[0];
for(size_t i=0; i<=say.size(); ++i)
{
if(last==say[i]) //每次与前一个字符比较是否相等,相等则计数加一
{
++count;
}
else
{
ss<<count<<last; //将count的值传递到流ss中,再将last的值传入
count=1;
last=say[i];
}
}
return ss.str(); //返回流中的字符串
}
string countAndSay(int n) {
if(n<=0) return string();
string say="1"; //定义初始字符串的内容
for (int i=1; i<n; ++i)
{
say=convert(say);
}
return say;
}
};

 或:

class Solution {
public:
string nextRead(string s) {
stringstream ss;
int count, i = 0, n = s.length();
while (i < n) {
count = 0;
while (i + 1 < n && s[i] == s[i + 1]) {
i++;
count++;
}
ss << count + 1 << s[i];
i++;
}
return ss.str();
}
string countAndSay(int n) {
string res = "";
if (n == 0) return res;
res = "1";
if (n == 1) return "1";
while (n > 1) {
res = nextRead(res);
n--;
}
return res;
}
};

  其他解法:

class Solution {
public:
string countAndSay(int n) {
if (n==0){
return NULL;
}
if (n==1){
return "1";
}
int count=1;
string s1="1";
string s2;
int j=1;
if (n>=2){
s1="11";
j=2;
}
int i=0;
while (j<n && n>1){
s2="";
for (i=0;i<s1.size();i++){ for (int m=i;m<s1.size()-1;m++){
if (s1[m]==s1[m+1]){
count++;
i++;
}
if (s1[m]!=s1[m+1]){
break;
}
}
s2+=to_string(count)+s1[i];
count=1; }
s1=s2;
j++;
} return s1;
}
};

  

class Solution {
public:
string countAndSay(int n) {
if(n==0) return " ";
if(n==1) return "1";
string str="1";
string tmp;
for(int i=1;i<n;++i)
{
tmp.clear();
int cnt=1;
for(int j=0;j<str.size();++j)
{
while(j+1<str.size())
{
if(str[j]==str[j+1])
{
++cnt;
++j;
}
else
break;
}
tmp.push_back(cnt+'0');
tmp.push_back(str[j]);
cnt=1;
}
str=tmp;
}
return str;
}
};

  

leetcode:Count and Say的更多相关文章

  1. leetcode:Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...

  2. leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  3. leetcode 315. Count of Smaller Numbers After Self 两种思路

    说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...

  4. LeetCode:二叉搜索树中第K小的数【230】

    LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...

  5. LeetCode:数据库技术【180-185】

    LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...

  6. LeetCode:数据库技术【175-178】

    LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...

  7. LeetCode:螺旋矩阵||【59】

    LeetCode:螺旋矩阵||[59] 题目描述 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ...

  8. LeetCode:旋转链表【61】

    LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...

  9. LeetCode:乘法表中的第K小的数【668】

    LeetCode:乘法表中的第K小的数[668] 题目描述 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要 ...

随机推荐

  1. 2014 ACM/ICPC Asia Regional Anshan Online

    默默的签到 Osu! http://acm.hdu.edu.cn/showproblem.php?pid=5003 #include<cstdio> #include<algorit ...

  2. 来自平时工作中的css知识的积累---持续补充中

    ① 现代浏览器中,<img>元素默认情况下底部会有空白,那么这个空白到底是从哪里来的? 解惑: method-one:猛戳 来自知乎的解答 method-two: 延伸阅读 what is ...

  3. PIX的使用

    这几天pix的一个问题可坑死我了,之前用的时候有蓝色的链接,点过去就可以查看相应资源,后来都是黑色的了没有可以点的链接. 看文档翻来翻去也没进展,后来找到了, 先打开render那个窗口 这样even ...

  4. DSP中常用的C语言关键字

    const Ø使用:const 数据类型 变量名: Ø作用:优化存储器的分配,表示变量的内容是常数,不会改变. Ø举例:const char tab[1024]={显示数据}; volatile(易变 ...

  5. C#中String 与Color之间的相互转换

    C#中String 与Color之间的相互转换 ————————————宋兴柱     其实,我们平常如果要在数据库存放Color类型值的话,肯定会在数据库中建立varchar类型.对吧.但是Colo ...

  6. android 关于Location of the Android SDK has not been setup in the preferences的解决方法

    今天在部署android开发环境的时候,每次打开eclipse的时候点击AVD Manager的按钮就会弹出Location of the Android SDK has not been setup ...

  7. eclipse 自动 注释

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...

  8. HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

    题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algori ...

  9. Gdata XML解析配置和简单使用

    导入libxml2,使用第三方AFNetworking网络请求,第三方XML解析GData GData需要的配置 Build Settings 里搜索,添加如下

  10. Eclipse里面开发ExtJS程序

    首先在http://extjs.org.cn/download下载ext ,我下载的版本是ext-3.4.1.1-gpl,下载好以后解压. 然后在eclipse里面先建立一个web项目,建立好后再we ...