-
Preface
- FAQ
-
Part I - Basics
- Basics Data Structure
- Basics Sorting
- Basics Algorithm
- Basics Misc
-
Part II - Coding
- String
-
Integer Array
-
Remove Element
-
Zero Sum Subarray
-
Subarray Sum K
-
Subarray Sum Closest
-
Recover Rotated Sorted Array
-
Product of Array Exclude Itself
-
Partition Array
-
First Missing Positive
-
2 Sum
-
3 Sum
-
3 Sum Closest
-
Remove Duplicates from Sorted Array
-
Remove Duplicates from Sorted Array II
-
Merge Sorted Array
-
Merge Sorted Array II
-
Median
-
Partition Array by Odd and Even
-
Kth Largest Element
-
Remove Element
-
Binary Search
-
First Position of Target
-
Search Insert Position
-
Search for a Range
-
First Bad Version
-
Search a 2D Matrix
-
Search a 2D Matrix II
-
Find Peak Element
-
Search in Rotated Sorted Array
-
Search in Rotated Sorted Array II
-
Find Minimum in Rotated Sorted Array
-
Find Minimum in Rotated Sorted Array II
-
Median of two Sorted Arrays
-
Sqrt x
-
Wood Cut
-
First Position of Target
-
Math and Bit Manipulation
-
Single Number
-
Single Number II
-
Single Number III
-
O1 Check Power of 2
-
Convert Integer A to Integer B
-
Factorial Trailing Zeroes
-
Unique Binary Search Trees
-
Update Bits
-
Fast Power
-
Hash Function
-
Happy Number
-
Count 1 in Binary
-
Fibonacci
-
A plus B Problem
-
Print Numbers by Recursion
-
Majority Number
-
Majority Number II
-
Majority Number III
-
Digit Counts
-
Ugly Number
-
Plus One
-
Palindrome Number
-
Task Scheduler
-
Single Number
-
Linked List
-
Remove Duplicates from Sorted List
-
Remove Duplicates from Sorted List II
-
Remove Duplicates from Unsorted List
-
Partition List
-
Add Two Numbers
-
Two Lists Sum Advanced
-
Remove Nth Node From End of List
-
Linked List Cycle
-
Linked List Cycle II
-
Reverse Linked List
-
Reverse Linked List II
-
Merge Two Sorted Lists
-
Merge k Sorted Lists
-
Reorder List
-
Copy List with Random Pointer
-
Sort List
-
Insertion Sort List
-
Palindrome Linked List
-
LRU Cache
-
Rotate List
-
Swap Nodes in Pairs
-
Remove Linked List Elements
-
Remove Duplicates from Sorted List
-
Binary Tree
-
Binary Tree Preorder Traversal
-
Binary Tree Inorder Traversal
-
Binary Tree Postorder Traversal
-
Binary Tree Level Order Traversal
-
Binary Tree Level Order Traversal II
-
Maximum Depth of Binary Tree
-
Balanced Binary Tree
-
Binary Tree Maximum Path Sum
-
Lowest Common Ancestor
-
Invert Binary Tree
-
Diameter of a Binary Tree
-
Construct Binary Tree from Preorder and Inorder Traversal
-
Construct Binary Tree from Inorder and Postorder Traversal
-
Subtree
-
Binary Tree Zigzag Level Order Traversal
-
Binary Tree Serialization
-
Binary Tree Preorder Traversal
- Binary Search Tree
- Exhaustive Search
-
Dynamic Programming
-
Triangle
-
Backpack
-
Backpack II
-
Minimum Path Sum
-
Unique Paths
-
Unique Paths II
-
Climbing Stairs
-
Jump Game
-
Word Break
-
Longest Increasing Subsequence
-
Palindrome Partitioning II
-
Longest Common Subsequence
-
Edit Distance
-
Jump Game II
-
Best Time to Buy and Sell Stock
-
Best Time to Buy and Sell Stock II
-
Best Time to Buy and Sell Stock III
-
Best Time to Buy and Sell Stock IV
-
Distinct Subsequences
-
Interleaving String
-
Maximum Subarray
-
Maximum Subarray II
-
Longest Increasing Continuous subsequence
-
Longest Increasing Continuous subsequence II
-
Maximal Square
-
Triangle
- Graph
- Data Structure
- Big Data
- Problem Misc
-
Part III - Contest
- Google APAC
- Microsoft
- Appendix I Interview and Resume
-
Tags
Count and Say
Tags: String, Easy
Question
- leetcode: Count and Say
- lintcode: Count and Say
Problem Statement
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 _n_th sequence.
Note: The sequence of integers will be represented as a string.
题解1 - 迭代
题目大意是找第 n 个数(字符串表示),规则则是对于连续字符串,表示为重复次数+数本身。那么其中的核心过程则是根据上一个字符串求得下一个字符串,从 '1'
开始迭代 n - 1 次即可。
Python
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n <= 0:
return ''
seq = '1'
for _ in range(n - 1):
seq = re.sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1), seq)
return seq
copy
Python
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n <= 0:
return ''
curr_seq = '1'
for j in range(n - 1):
curr_seq = self._get_next_seq(curr_seq)
return curr_seq
def _get_next_seq(self, seq):
next_seq = ''
cnt = 1
for i in range(len(seq)):
if i + 1 < len(seq) and seq[i] == seq[i + 1]:
cnt += 1
else:
next_seq += str(cnt)
next_seq += seq[i]
cnt = 1
return next_seq
copy
C++
class Solution {
public:
string countAndSay(int n) {
if (n <= 0) return "";
string curr_seq = "1";
while (--n) {
curr_seq = getNextSeq(curr_seq);
}
return curr_seq;
}
private:
string getNextSeq(string seq) {
string next_seq = "";
int cnt = 1;
for (int i = 0; i < seq.length(); i++) {
if (i + 1 < seq.length() && seq[i] == seq[i + 1]) {
cnt++;
} else {
next_seq.push_back('0' + cnt);
next_seq.push_back(seq[i]);
cnt = 1;
}
}
return next_seq;
}
};
copy
Java
public class Solution {
public String countAndSay(int n) {
assert n > 0;
StringBuilder currSeq = new StringBuilder("1");
for (int i = 1; i < n; i++) {
currSeq = getNextSeq(currSeq);
}
return currSeq.toString();
}
private StringBuilder getNextSeq(StringBuilder seq) {
StringBuilder nextSeq = new StringBuilder();
int cnt = 1;
for (int i = 0; i < seq.length(); i++) {
if (i + 1 < seq.length() && seq.charAt(i) == seq.charAt(i + 1)) {
cnt++;
} else {
nextSeq.append(cnt).append(seq.charAt(i));
cnt = 1;
}
}
return nextSeq;
}
}
copy
源码分析
字符串是动态生成的,Python 中 next_seq
使用了两次 append 而不是字符串直接拼接,实测性能有一定提升,C++ 中对整型使用了 push_back('0' + cnt)
, 容易证明 cnt
不会超过3,因为若出现1111
,则逆向可得两个连续的1,而根据规则应为21
,其他如2222
推理方法类似。Java 中使用 StringBuilder 更为合适。除了通常方法外还可以使用正则表达式这一利器!
复杂度分析
略,与选用的数据结构有关。
题解2 - 递归
注意递归终止条件即可,核心过程差不多。
Python
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n <= 0:
return ''
if n == 1:
return '1'
seq = self.countAndSay(n - 1)
next_seq = ''
cnt = 1
for i in range(len(seq)):
if i + 1 < len(seq) and seq[i] == seq[i + 1]:
cnt += 1
else:
next_seq += str(cnt)
next_seq += seq[i]
cnt = 1
return next_seq
copy
C++
class Solution {
public:
string countAndSay(int n) {
if (n <= 0) return "";
if (n == 1) return "1";
string seq = countAndSay(n - 1);
string next_seq = "";
int cnt = 1;
for (int i = 0; i < seq.length(); i++) {
if (i + 1 < seq.length() && seq[i] == seq[i + 1]) {
cnt++;
} else {
next_seq.push_back('0' + cnt);
next_seq.push_back(seq[i]);
cnt = 1;
}
}
return next_seq;
}
};
copy
Java
public class Solution {
public String countAndSay(int n) {
assert n > 0;
if (n == 1) return "1";
String seq = countAndSay(n - 1);
StringBuilder nextSeq = new StringBuilder();
int cnt = 1;
for (int i = 0; i < seq.length(); i++) {
if (i + 1 < seq.length() && seq.charAt(i) == seq.charAt(i + 1)) {
cnt++;
} else {
nextSeq.append(cnt).append(seq.charAt(i));
cnt = 1;
}
}
return nextSeq.toString();
}
}
copy
源码分析
判断相邻字符是否相同时需要判断索引是否越界。
复杂度分析
略,与选用的数据结构有关。