这个还是一开始没读懂题目,题目如下:

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"代表一个1,依次递推,然后给一个数,然后返回在这个规律下的第n个数。【我之前看成了返回这种表达方式的n】【泪目】

因为考虑了递归的低效性,所以我打算用迭代来做,噢,对了,顺便说个坑,我在把int转string的时候用到了itoa这个工具函数,然后VS告诉我itoa不是安全函数请使用_itoa,在我改了后又给我弹出error说_itoa也不是安全的了,请使用_itoa_s,当我在VS中调试正常后,结果发现leetcode的OJ不支持_itoa_s。。。。。

所以我就手写了一个itoa,虽然不算太高效,但是还是能用的。这个题目的题解如下:

class Solution {
public:
string itoa_better(int n)
{
string tmp = "";
while (n != 0)
{
tmp += (n % 10 + '0');
n = n / 10;
}
reverse(tmp.begin(), tmp.end());
return tmp;
} string Say(string n)
{
char word[1] = { n[0] };
int sum = 1;
string Say = "";
string Sum = ""; for (string::iterator i = n.begin() + 1; i != n.end(); i++)
{
if (*i != word[0])
{
Say += itoa_better(sum) + word[0];
sum = 0;
word[0] = *i;
}
sum += 1;
}
Say += itoa_better(sum) + word[0]; return Say;
} string countAndSay(int n)
{
string tmp = "1"; for (int i = 0; i < n - 1; i++)
{
tmp = Say(tmp);
} return tmp;
}
};

[leetcode] 19. Count and Say的更多相关文章

  1. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  2. 【LeetCode】Count and Say(报数)

    这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...

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

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

  4. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  5. leetcode@ [327] Count of Range Sum (Binary Search)

    https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...

  6. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

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

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

  8. leetcode 730 Count Different Palindromic Subsequences

    题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...

  9. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

随机推荐

  1. express + mongodb 搭建一个简易网站(二)

    express + mongodb 搭建一个简易网站 (二) 在搭建网站(一)中,实现了简单的路由功能,这离一个完整的网站还差的有点远,继续撸代码吧. 1.首先在根目录下新建一个views文件夹,用来 ...

  2. https://developer.mozilla.org/

    Document/querySelector      https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector

  3. 大型运输行业实战_day04_2_高级查询

    1.高级查询最总效果 2.高级查询步骤 2.1页面输入框显示 开始车站:<input id="startStation" type="text" valu ...

  4. java写简单Excel 首行是目录 然后前台下载

    页面: <form action="${path}/xxx/xxx.do" method="get" > 表格下载:<input type=& ...

  5. ping,telnet,tracert分别用的是什么协议

    Telnet使用的是tcp协议使用示例:telnet 192.168.1.20 80 ping命令使用的是icmp协议示例:ping www.sina.com.cn或ping 192.168.1.10 ...

  6. 结对项目3-bug的三种状态

    这周和小伙伴结对构造程序,来深刻理解软件测试中,bug发现的三种状态. 1:不能触发Fault 2:触发Fault,但是不能触发Error 3:触发Error,但是不能产生Failure 我们完成的代 ...

  7. OpenCV(图像处理)—访问像素的三种方法

    方法一:用指针访问像素 #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include < ...

  8. linux下静默安装 weblogic12.2.1.3中间件

    第一步:准备需要的安装包文件,这里注意 weblogic12.2.*需要的最低JDK版本为1.8以上 1.查看当前的镜像列表:docker images 2.运行镜像,并进入容器,这里运行基本的cen ...

  9. java的PDF纵横向打印

    PDF默认是纵向打印的,通过rotate()来让其改变为横向打印,一般在打印A4 12*21纸以及发票的时候会用横向打印.横向打印时页面会出现行转列以及列转行的情况,因此在设置页面大小的时候一定要宽度 ...

  10. js深拷贝、浅拷贝

    浅拷贝: 只针对当前对象的属性进行拷贝,若当前对象的属性是引用类型时,这个不考虑,不进行拷贝.若属性是引用类型,拷贝后引用的是地址,如果进行更改,会影响拷贝的原对象属性. 深拷贝:针对当前对象的数据的 ...