LeetCode-38. Count and Say
2016-04-14 23:50
423 查看
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
这道题就是N次循环~然后每一次遍历一遍字符串即可。
1, 11, 21, 1211, 111221, ...
1is read off as
"one 1"or
11.
11is read off as
"two 1s"or
21.
21is 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.
这道题就是N次循环~然后每一次遍历一遍字符串即可。
public String countAndSay(int n) { String str = "1"; int cnt = 1; String temp = ""; int i = 1; for(int j = 0; j < n-1; j++){ char[] ch = str.toCharArray(); for(i = 1; i < str.length(); i++){ if(ch[i-1] == ch[i]){ cnt++; }else{ temp = temp + cnt + ch[i-1]; cnt = 1; } } temp = temp + cnt + ch[i-1]; str=temp; temp = ""; cnt = 1; } return str; }
相关文章推荐
- 【BZOJ1069】【SCOI2007】最大土地面积 计算几何 凸包
- java--正则表达式——邮箱规范
- 写日报
- linux脚本编程之条件判断
- 20145240 《Java程序设计》第二次实验报告
- mysql中表名是order的CRUD的错误
- 实例 20 重定向输出流实现程序日志
- Akka在Flink中的使用剖析
- 故障案例--mongodb添加副本集成员报错
- ASP.NET MVC之分部视图和ChildAction(三)
- JavaScript函数劫持
- Android中Service类onStartCommand
- poj 1419 dfs
- 括号匹配
- 括号匹配
- Windows Server 2012 R2 设置 NTP 服务
- c++的this指针
- Fibonacci Number
- 数组剔除元素后的乘积
- Community Value再理解