LeetCode 38 Count and Say
2016-03-30 15:16
253 查看
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.
题意怎么说就怎么做吧。暂时没找到更简便的方式。
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.
题意怎么说就怎么做吧。暂时没找到更简便的方式。
public static String countAndSay(int n) { if (n == 0) return null; String[] nums = new String[n - 1]; nums[0] = "1"; for (int i = 0; i < n - 1; i++) { int count = 0; char num = nums[i].charAt(0); StringBuffer say = new StringBuffer(""); for (int j = 0; j < nums[i].length(); j++) { if (nums[i].charAt(j) == num) count++; else { say.append(count).append(num); num = nums[i].charAt(j); count = 1; } } nums[i + 1] = say.append(count).append(num).toString(); } return nums[n - 1]; }
相关文章推荐
- java 抽象方法
- hibernate 4.3.X遇到的一些问题收集
- mysql的类dblink方案
- java正则表达式
- linux动态库编译和使用
- 动物名称日语单词集合
- JAVA springMVC学习笔记
- 静态内部类与非静态内部类
- springMVC访问静态资源
- 第五周项目1.4-三角形类雏形
- C#委托的介绍(delegate、Action、Func、predicate)
- Go net包介绍
- ubuntu14.04 + opencv3.1 + contrib modules
- aop自定义切面
- Activity 的 LaunchMode
- IntentService
- Linux(2):Linux系统关闭与重启
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
- Linux目录结构剖析说明
- 每天一个linux命令(5):xargs命令