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

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. conductor Workflow Metrics

    Server Metrics conductor使用spectator收集指标.https://github.com/Netflix/spectator 名称 目的 标签 workflow_serve ...

  2. zabbix超级乱码解决问题

    个人而言,网上那些方法并不顶什么用,研究了一番总结出了自己的一个方法. 像这种,出现了严重乱码,也不是像网上说的那样把楷体复制过去就好.事实证明我复制过去好多次了,然而并没有什么用. 下面是我自己总结 ...

  3. 获取RequestMapping注解中的属性

    参考:https://www.cnblogs.com/2013jiutian/p/7294053.html @RequestMapping("/value1") @Controll ...

  4. valgrind: failed to start tool 'memcheck' for platform 'amd64-linux': No such file or directory

    这是没有配置lib环境变量的缘故. 注意要把lib配置放到path配置前面. 导出VALGRIND_LIB路径,用法如下(假设valgrind已经被安装到/home/test/valgrind目录): ...

  5. iOS - 上架的APP 生成二维码下载

    1.首先打开苹果App Store商店进入到里面,找到需要打开链接地址的应用程序,例如:百度. 2. 在App Store商店里面先点击一下应用程序图标,再按一下…分享按钮. 3. 接着选择分享APP ...

  6. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. Zabbix监控PostgreSQL

    目录 Zabbix监控PostgreSQL 1. 安装libzbxpgsql 2. 配置zabbix配置文件zabbix_agentd.conf 3. 创建监控用户 4. 导入监控模板 5. 主机链接 ...

  8. Docker常见问题

    问题 当我使用docke search mysql时,显示如下错误: [root@iZ25u61v97hZ opt]# docker search redis Segmentation Fault o ...

  9. win 下 nginx 与 php的配置

    1.下载需要的软件包 php的windows版本(*注意这里下载非线程安全的,nginx使用的是cgi) http://windows.php.net/download/   nginx的window ...

  10. php在线支付流程

    1.企业与银行的两种接入方式: (1).企业直接与银行对接. 优点:直接与银行进行财务结算,资金安全,适合资金流较大企业.         缺点:开发和维护工作量较大,分别与每家银行签订合同,每年需交 ...