Leetcode Count and Say
来源:互联网 发布:微商和淘宝哪个挣钱 编辑:程序博客网 时间:2024/06/11 18:28
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.
Difficulty: Easy
public class Solution { public String helper(String s, int dep){ if(dep == 0 || s.length() == 0) return s; String ans = ""; int count = 1; char c = s.charAt(0); for(int i = 1; i < s.length();i++){ if(s.charAt(i) != c){ ans = ans + Integer.toString(count) + c; count = 1; c = s.charAt(i); } else{ count++; } } ans = ans + Integer.toString(count) + c; return helper(ans, dep - 1); } public String countAndSay(int n) { return helper("1", n - 1); }}
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
- Leetcode Reorder List
- Leetcode Palindrome Number
- 大笨鹰
- Leetcode Invert Binary Tree
- Leetcode Add Digits
- Leetcode Count and Say
- Leetcode Container With Most Water
- Leetcode Remove Element
- Leetcode Remove Duplicates from Sorted Array
- Leetcode Rotate Array
- Leetcode Merge Sorted Array
- Leetcode Find Minimum in Rotated Sorted Array
- Leetcode Reverse String
- Leetcode Happy Number