[LeetCode-Java]38. Count and Say
来源:互联网 发布:python 查看字节码 编辑:程序博客网 时间:2024/05/17 01:55
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.
解:递归的思想以及利用StringBuilder对统计的数据进行连接。
public class Solution { public String countAndSay(int n) { if (n == 1){ return "1"; } String temp = countAndSay(n-1); int count = 1; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0;i < temp.length();i++){ //注意对最后一位的处理 if (i == temp.length() - 1){ if (count>1){ stringBuilder.append(count); stringBuilder.append(temp.charAt(i-1)); }else { stringBuilder.append(count); stringBuilder.append(temp.charAt(i)); } break; } if (temp.charAt(i+1) == temp.charAt(i)){ count ++; }else { stringBuilder.append(count); stringBuilder.append(temp.charAt(i)); count = 1; } } return stringBuilder.toString(); }}
0 0
- Leetcode:38. Count and Say(JAVA)
- [LeetCode-Java]38. Count and Say
- [leetcode]38. Count and Say(java)
- [LeetCode] 38. Count and Say java
- [leetcode]38. Count and Say@Java
- [Leetcode] Count and Say (Java)
- [LeetCode][Java] Count and Say
- count and say leetcode java
- LeetCode : Count and Say [java]
- LeetCode Count and Say(java)
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- Leetcode 38. Count and Say
- leetcode 38. Count and Say
- [leetcode] 38. Count and Say
- HDU2553 N皇后问题
- Android之日历源码浅析
- 转义字符、break、continue关键字
- JSPWiki中文乱码问题
- Linux - C进程间通信(预习内容六)
- [LeetCode-Java]38. Count and Say
- Android图表绘制之MPAndroidChart
- 大山中学某模拟赛
- TabLayout实现每个tab有一个不同的图片,选中改变图片
- 1~n的整数中1的个数
- Git和SVN的区别
- day7_表单
- 【UVA 11426】 【求sum(gcd(i,j),1<=i<j<=n)1<n<4000001】
- 2016年9月23日19:49:33