LeetCode(38)题解: Count and Say
https://leetcode.com/problems/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.
思路:
题意有些模糊,看明白1211到111221就懂了。
AC代码:
class Solution {
public:
string countAndSay(int n) {
int tmp_n,count,k;
string tmp,tmp_res,a[n];
a[]="";
for(int i=;i<n;i++){
tmp=a[i-];
tmp_res="";
tmp_n=tmp.size();
k=;
count=;
for(int j=;j<tmp_n;j++){
if(tmp[j]==tmp[j-]){
count++;
continue;
}
else{
tmp_res.push_back(''+count);
tmp_res.push_back(tmp[j-]);
count=;
}
}
tmp_res.push_back(''+count);
tmp_res.push_back(tmp[tmp_n-]);
a[i]=tmp_res;
}
return a[n-];
}
};
LeetCode(38)题解: Count and Say的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【LeetCode算法-38】Count and Say
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- 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 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
随机推荐
- 【编程工具】如何用Sublime Text3建立本地服务器和站点
不久前,我学习了使用DW(DreamWare CS6)这款软件来学习HTML和制作静态网页,但是总觉得这款图形化软件不太适合我这种喜欢写代码的码农,所以最终我使用的是Sublime Text3这款软件 ...
- Masonry练习
tableView的cell自动适应,scrollview自动适应,自定义自动布局控件 demo链接:http://pan.baidu.com/s/1jHsrGwQ
- 【Luogu】P1417烹调方案(排序01背包)
题目链接 对食材进行排序,重载运算符代码如下: struct food{ long long a,b,c; bool operator <(const food &a)const{ re ...
- POJ 1006 生理周期【数论】
这题是有中文版的(右上角选项卡里把default改成简体中文)然后看到他把biorhythms翻成生理周期我可耻的笑了......23333 如果没有限定从日期d开始,完全可以从第一天起开始计时,因此 ...
- 【BZOJ1412】狼和羊的故事(最小割)
题意:将一个由0,1,2构成的矩阵里的1与2全部分割最少需要选取多少条边 n,m<=100 思路:裸的最小割模型 相邻的格子连容量为1的边(其实可以少连很多遍,1与1,2与2之间的边是没有意义的 ...
- Fabrice Bellard其人 ---- FFMPEG及其他……
有些计算机科学家的名字耳熟能详:阿兰·图灵(Alan Turing).高纳德(Donald Knuth).艾兹赫尔·戴克斯特拉(Edsger Dijkstra),这些人的名气甚至大于他们突破性的成就. ...
- Linux java 启动脚本
#!/bin/bash export LANG=en_US.UTF8 start(){ ulimit -n 65535 #find the jars jar_lib=`ls -1 lib/*.jar` ...
- [转发]Android 系统稳定性 - ANR(三)
文章都为原创,转载请注明出处,未经允许而盗用者追究法律责任. 很久之前写的了,留着有点浪费,共享之. 编写者:李文栋 http://rayleeya.iteye.com/blog/1956056 1. ...
- xrandr
ubuntu 外接显示器 xrandr常用命令(这里的VGA与LVDS分别换成第1步中的设备名,如VGA1.LVDS1): xrandr --output VGA --same-as LVDS --a ...
- Java常用几种加密算法
对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去.收信方收到密文后,若想解 ...