leetcode-Count and Say
2017-12-30 11:10
330 查看
The count-and-say sequence is the sequence of integers with the first five terms as following:
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.
主要是要理解题目的意思,其次注意字符串连接末尾的情况,给出的程序处理的挺好的
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.
class Solution { public String countAndSay(int n) { if(n==1) return "1"; String str = countAndSay(n-1); String res = new String(); for(int i=0; i<str.length();i++ ){ int count = 1; while(i<str.length()-1 && str.charAt(i)==str.charAt(i+1)){ i++; count++; } res = res+String.valueOf(count); res = res+String.valueOf(str.charAt(i)); } return res; } }
主要是要理解题目的意思,其次注意字符串连接末尾的情况,给出的程序处理的挺好的
相关文章推荐
- 38.[Leetcode]Count and Say
- 37 leetcode - Count and Say
- Leetcode 38 Count and Say
- LeetCode Count And Say
- leetcode-38-count and say
- Count and say[easy on LeetCode]
- 【leetcode】Count and Say
- Leetcode 38 Count and Say 传说中的递推
- LeetCode(51)- Count and Say
- [LeetCode]Count and Say
- LeetCode38 Count and Say
- Leetcode: Count and Say
- int转string与LeetCode:Count and Say
- [leetcode]Count and Say
- [LeetCode]题解(python):038-Count and Say
- [C++]LeetCode: 20 Count and Say
- LeetCode(38)题解: Count and Say
- LeetCode_Count and Say
- LeetCode: Count and Say
- [LeetCode] Count and Say