[leetcode] 19. Count and Say
这个还是一开始没读懂题目,题目如下:
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"
or11
.
11
is read off as"two 1s"
or21
.
21
is 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 ...
随机推荐
- /Library,/System/Library,~/Library
/System/library是系统级,/ Library下面面向全部用户,~/Library 限于当前用户 一般来说,很少碰/ Library,都是用到/System/library和~/lib ...
- mysql数据库的卸载
1.控制面板 程序和功能 卸载MySQL相关 2.卸载MySQL的安装目录 与储存目录 3.删除C盘下隐藏MySQL文件:组织-----文件夹和搜索选项-----------查看------ ...
- TensorFlow—多层感知器—MNIST手写数字识别
1 import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import ...
- 关于"undefined reference"错误
这个错误换句话说: 链接的时候找不到实现的文件(谨记从这个入手!). 可能导致的原因有: 1. 没有链接库文件,包括静态库或动态库. 2. 链接文件的顺序问题,先后依赖问题,把被依赖的放后面. 3. ...
- 二叉树的最大/小/平衡 深度 depth of binary tree
[抄题]: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. [思维问题]: [一句话思路]: 分合法的定义 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况 ...
- [leetcode]277. Find the Celebrity 找名人
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- Visual Event查看页面相关绑定事件
页面相关绑定的事件比较复杂,在不熟悉的情况下很难找到相关逻辑的位置,所以希望借助工具来帮自己理清相关事件的脉络走向. 浏览器 工具 chrome( 58.0.3029.110) Visual Even ...
- 201621123008 《Java程序设计》第五周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键词:接口,内部类. 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 1.3 可选:使用常 ...
- MySql 几个小技巧
分页查看: 在 mysql 环境下,执行命令: pager more,之后的结果分屏了. 简明扼要地查看表结构: describe table_name
- [转]微信公众平台(测试接口)开发前的准备工作(转载自walkingmanc的专栏)
本文转自:http://blog.csdn.net/jiangweicpu/article/details/21228949 http://blog.csdn.net/walkingmanc/arti ...