Count and Say
来源:互联网 发布:恒生电子软件开发升迁 编辑:程序博客网 时间:2024/05/16 18:29
参考:http://blog.csdn.net/linhuanmars/article/details/20679963
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.
实际在跑的时候,内层的循环当进行到最后一次的时候,只会计数而不会往res上添加结果,所以要在内层循环外再补上。
代码:
package leetcode;public class CountAndSay {public static void main(String args[]){System.out.println(new CountAndSay().countAndSay(4));}public String countAndSay(int n) {if (n < 0)return "";String curRes = "1";for (int i = 2; i <= n; i++) {int count = 1;String res = "";for (int j = 1; j < curRes.length(); j++) {if (curRes.charAt(j - 1) == curRes.charAt(j)) {count++;} else {res += count;res += curRes.charAt(j - 1);count = 1;}}//举个例子就懂了.. res += count; res += curRes.charAt(curRes.length()-1); curRes = res;}return curRes;}}
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- Count and Say
- [38]Count and Say
- Count and Say
- Count and Say
- Count and Say
- [LeetCode]Count and Say
- Count and Say
- Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- Nginx - Windows下Nginx基本安装和配置
- Pascal实用代码模板
- UIButton2
- 笔记本联想(Lenovo)G40-70M加装内存和SSD固态硬盘
- win 7下使用IIS搭建FTP服务器和添加用户访问
- Count and Say
- 如何在VC++中使用Windows Forms (VS2012-VS2015)
- 【随便写写】为什么可以O(m)地得到C(n,m)
- storm trident merger
- leetcode 164: Maximum Gap
- HEX文件格式详解
- HDU5407 CRB and Candies 数论
- 2000年-2010年各省回族人口增长率
- 黑马程序员-------Java笔记--------怎么越过泛型检查