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. 项目分析(map复习)

    有段时间没看map里面的东西了,刚才看发现功能上增加了一些,在来复习了一次流程初始化每个map建立线程,这个线程有两个功能,1.处理GS发过来的包 2.驱动map里面的定时器GS发过来的包是存在m_g ...

  2. 查看Centos系统信息命令

    linux命令行具有强大的功能,我们安装vps后,首先应该知道系统信息,查看这些信息,你会发现Linux命令很简单,你可以按照下面的命令练习. linux系统信息 # uname -a # 查看内核/ ...

  3. codeforces #235div2 D

    完全没看出是状态压缩DP, 果然没练习,之前一直再看,看来要把状压做几道了, 上代码吧:代码也是问道的 无语... #include<cstdio> #include<cstring ...

  4. Isomorphic JavaScript: The Future of Web Apps

    Isomorphic JavaScript: The Future of Web Apps At Airbnb, we’ve learned a lot over the past few years ...

  5. 您可能不知道的ASP.Net小技巧

    <!-- 页码和简介 --> 1.  在提交页面之后,保持滚动条的位置 可以在page指令上加上MaintainScrollPositionOnPostback指令 <%@ Page ...

  6. eclipse git 整合

    最近朋友都推荐使用github管理自己的项目,而且免费用户可以有5个仓库,恰好我也想了解下git,借此机会学习一下.github官方指南使用独立第三方git工具来进行版本控制,并不借助于eclipse ...

  7. Firefly卡牌手游《暗黑世界V1.5》服务器端源码+GM管理后台源码

    http://www.9miao.com/content-6-304.html Firefly卡牌手游<暗黑世界V1.5>服务器端源码+GM管理后台源码 关于<暗黑世界V1.5> ...

  8. mysql5日期类型datetime查询范围值

    1.DATE_FORMAT函数 SELECT a.create_time FROM account_log a WHERE a.create_time >= DATE_FORMAT('2014- ...

  9. HDU2295 Radar (DLX)

    下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...

  10. HDU 3998 Sequence (最长上升子序列+最大流)

    参考链接:http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html 题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子 ...