【LeetCode】Count and Say
2014-05-06 14:43
316 查看
Count and Say
Total Accepted: 7984 Total Submissions: 30169 My Submissions
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)、1
2)、11,表示1)有1个1,组合起来就是11。
3)、21,表示2)有2个1,组合起来就是11。
4)、1211,表示3)有1个2,2个1,组合起来就是1121。
5)、111221,表示4)有1个1,1个2,2个1,组合起来就是111221。
6)、312211,表示5)有3个1,2个2,2个1,组合起来就是312211
.......
以此类推。
其实就是统计连续的相同的字符的个数。考察基本功。
Java AC
Total Accepted: 7984 Total Submissions: 30169 My Submissions
The count-and-say sequence is the sequence of integers beginning as follows:
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.
【解题思路】
看了好几次都没有看懂题目意思,果然是学渣。
恩,我需要解释一下。
1)、1
2)、11,表示1)有1个1,组合起来就是11。
3)、21,表示2)有2个1,组合起来就是11。
4)、1211,表示3)有1个2,2个1,组合起来就是1121。
5)、111221,表示4)有1个1,1个2,2个1,组合起来就是111221。
6)、312211,表示5)有3个1,2个2,2个1,组合起来就是312211
.......
以此类推。
其实就是统计连续的相同的字符的个数。考察基本功。
Java AC
public class Solution { public String countAndSay(int n) { String array[] = new String ; array[0] = "1"; for(int i = 1; i < n; i++){ char strArr[] = array[i-1].toCharArray(); int len = strArr.length; StringBuilder sb = new StringBuilder(); int j = 0; while(j < len){ int k = j + 1; int count = 1; while(k < len && strArr[k] == strArr[j]){ k++; count++; } sb.append(count); sb.append(String.valueOf(strArr[j])); j = k; } array[i] = sb.toString(); } return array[n-1]; } }
相关文章推荐
- leetcode——38——Count and Say
- [LeetCode] Count and Say
- [leetcode] Count and Say
- 【leetcode c++】38 Count and Say
- LeetCode---Count and Say
- leetcode_c++:Count and Say(038)
- leetcode之路038 Count and Say
- leetcode Count and Say
- LeetCode-38-Count and Say Python的int_to_string
- 【leetcode】Count and Say
- leetcode Count and Say
- LeetCode刷题(C++)——Count and Say(Easy )
- Leetcode 38 Count and Say
- 《leetCode》:Count and Say
- leetcode_Count and Say
- 【Leetcode】【python】Count and Say
- LeetCode-38-Count and Say-E
- leetcode-38-Count and Say
- leetcode 38: Count and Say
- LeetCode Online Judge 题目C# 练习 - Count and Say