[leetcode] 19. Count and Say
这个还是一开始没读懂题目,题目如下:
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.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的更多相关文章
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- 【LeetCode】Count and Say(报数)
这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- 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 ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- leetcode 730 Count Different Palindromic Subsequences
题目链接: https://leetcode.com/problems/count-different-palindromic-subsequences/description/ 730.Count ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
随机推荐
- 新手C#SQLServer在程序里实现语句的学习2018.08.12
从C#中连接到SQL Server数据库,再通过C#编程实现SQL数据库的增删改查. ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据 ...
- jsp 不显示换行 Eclipse复制一行快捷键
jsp通过out.println();不能换行.html中需要标签<br/>才可以 Eclipse 复制整行代码移动:Ctrl+Alt+↑/↓.但是跟系统屏幕上下切换冲突,可以选择,在电脑 ...
- sqldatareader无法得到output参数的解决
只需要在所有的sqldatareader结束后,加上一句就可以得到输出参数了. sdr.Close(); Object ObjCount = cmd.Parameters["@Count_P ...
- 第六章 图(b1)邻接矩阵
- zoj1109-Language of FatMouse 【字典树】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...
- 55. Jump Game (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- CSS文字大小单位px、em、pt详解
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...
- oracle在centos6.5安装
说明 很多操作是默认,具体定制另说. 安装 参考http://www.linuxidc.com/Linux/2014-02/97374p4.htm 这篇是上面那篇的整合版:http://www.cnb ...
- access数据库收缩(压缩)
一般是因为表中有大量没用的数据,把没用的数据全部删除 菜单栏的“工具”——“数据库实用工具”——“压缩和修复数据库” OK啦
- PHP下ajax跨域的解决方案之jsonp
首先要说明一下json和jsonp的区别? json是一种基于文本的数据交换方式,或者叫做描述数据的一种格式. var person = { "name": "test& ...