leetcode 38
38. 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" 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被读作“1个1” 或 11;11被读作“2个1”或21;21被读作“1个2,1个1”或1211;依次这样下去,输出第n个序列。
代码如下:
class Solution {
public:
string countAndSay(int n) {
string first = "";
for(int i = ; i < n; i++)
{
string ss = "";
int count = ;
for(int j = ; j < first.length(); j++)
{
if(first[j-] == first[j])
{
count ++;
}
else
{
ss = ss + (char)(count + '') + first[j-];
count = ;
}
}
first = ss + (char)(count + '') + first[first.length()-];
}
return first;
}
};
leetcode 38的更多相关文章
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java实现 LeetCode 38 外观数列
38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- LeetCode(38)题解: Count and Say
https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integ ...
- Leetcode 38.报数 By Python
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...
- [leetcode] 38. 报数(Java)(字符串处理)
38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
随机推荐
- [物理学与PDEs]第1章习题参考解答
[物理学与PDEs]第1章习题1 无限长直线的电场强度与电势 [物理学与PDEs]第1章习题2 均匀带电球面的电场强度与电势 [物理学与PDEs]第1章习题3 常场强下电势的定解问题 [物理学与PDE ...
- Transact-SQL的除法问题
SELECT 3800/365, 3800.0/365; 执行上面的sql,得到的结果是:10, 10.410958 返回优先级较高的参数的数据类型. 有关详细信息,请参阅数据类型优先级 (Trans ...
- JAVA while循环,do-while循环,for循环
一.while循环 实例: public class Test{ public static void main(String[] args){ int i = 1; while(i<30){ ...
- Guava 10-散列
概述 Java内建的散列码[hash code]概念被限制为32位,并且没有分离散列算法和它们所作用的数据,因此很难用备选算法进行替换.此外,使用Java内建方法实现的散列码通常是劣质的,部分是因为它 ...
- 解决mysql"Access denied for user'root'@'IP地址'"问题
在另一台服务器使用 MySQL-Front链接时: 解决方法: 在MySQL服务器上使用root登录后,执行如下SQL语句: mysql 登录命令: >mysql -u root -p; 然后执 ...
- Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services
Citrix 服务器虚拟化之二十一 桌面虚拟化之部署Provisioning Services Provisioning Services 是Citrix 出品的一系列虚拟化产品中最核心的一个组件, ...
- Golang 图片上绘制文字
之前介绍过使用 FreeType-go 来绘制字(http://www.cnblogs.com/ghj1976/p/3445568.html), 现在相关的包被做了整合,主要是整合到了 github ...
- CentOS 6 安装 Python3.5以及配置Django
http://www.jianshu.com/p/6199b5c26725 文/FiveStrong(简书作者)原文链接:http://www.jianshu.com/p/6199b5c26725著作 ...
- 蓝桥杯---波动数列(dp)(背包)(待解决)
问题描述 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减 ...
- 【转】android的一些开源项目
自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会不定期的更新这篇文章. ...