Leetcode || Count and Say
来源:互联网 发布:婚纱摄影网络销售技巧 编辑:程序博客网 时间:2024/05/18 01:01
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.
package pack;class Solution { public String countAndSay(int n) { if(n == 0) return null; String result = "1"; //第一个为"1" for(int i=2; i<=n; i++) { //从第二个开始,每个都以前一个做参数 result = countAndSay(result); } return result; } public String countAndSay(String str) { str += "x"; //最后增一个,便于考虑原本最后一位 int repeat_times = 0; //重复次数 String result = ""; for(int i=0; i<str.length()-1; i++) { if(str.charAt(i+1) == str.charAt(i)) { repeat_times++; } if(str.charAt(i+1)!=str.charAt(i) && repeat_times==0) { result = result + "1" + str.charAt(i); } if(str.charAt(i+1)!=str.charAt(i) && repeat_times>0) { result = result + (repeat_times+1) + str.charAt(i); repeat_times = 0; } } return result; }}public class Mian { public static void main(String[] args) { System.out.println(new Solution().countAndSay(3)); }}
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- iOS 手势识别器
- PHP面向对象:延迟静态绑定(static关键字)
- java.net.ConnectException: Connection refused问题解决办法
- xcode比较有用的插件和下载
- 奇异值分解压缩图像SVD
- Leetcode || Count and Say
- 顺序表应用4:元素位置互换之逆置算法
- 使用ngin实现tomcat6和iis共用80端口
- NSDate/NSData
- 【字典树】HDU1671Phone List(论释放内存的重要性)
- 怎样实现android 返回到上一个Activity并重新执行一次onCreate方法
- 被误解的MVC和被神化的MVVM
- 顺序表应用5:有序顺序表归并
- swift学习Day05