lintcode-easy-Count and Say
2016-02-24 17:31
260 查看
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer
Example
Given n =
Note
The sequence of integers will be represented as a string.
思路也很直接,但是写的时候要注意数组index越界一类的问题
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.
Example
Given n =
5, return
"111221".
Note
The sequence of integers will be represented as a string.
思路也很直接,但是写的时候要注意数组index越界一类的问题
public class Solution { /** * @param n the nth * @return the nth sequence */ public String countAndSay(int n) { // Write your code here if(n < 1) return null; if(n == 1) return "1"; String prev = "1"; String next = ""; for(int i = 2; i <= n; i++){ char[] temp = prev.toCharArray(); int index = 0; while(index < temp.length){ int count = 0; count++; index++; while(index < temp.length && temp[index] == temp[index - 1]){ count++; index++; } next = next + String.valueOf(count) + temp[index - 1]; } prev = next; next = ""; } return prev; } }
相关文章推荐
- sublime 3103liense
- LCD时序中设计到的VSPW/VBPD/VFPD/HSPW/HBPD/HFPD总结
- JAVA正则表达式语法大全
- window.location.hash详解
- HttpSession 的作用
- 线条属性
- UGUI UI框架实现遇到的问题
- 编译android源码
- 周记(搭建cas验证服务)
- IOS OC声明变量在@interface括号中与使用@property的区别
- 【linux】curl 速记
- mysql 之 主从双机 加 keepalived
- linux命令大全
- ios 几天后到期
- Android Binder IPC分析
- Android 获取SD卡路径和判断SD卡是否存在.
- android Mp3播放器之音频文件扫描
- UVa 12563 Jin Ge Jin Qu hao dp : 0-1背包问题
- JavaScript
- php-fpm