leetcode #38 in cpp
2016-05-26 05:31
232 查看
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.
Solution:
for 1 to n:
Set current string = "";
Given a string, count the frequency of every unique number. Then append the (frequency in string) + (the number in string) to current string.
Code:
class Solution {
public:
string countAndSay(int n) {
string s = "1";
return count_and_say(s,n);
}
string count_and_say(string s, int n){
if(n == 1) return s;
vector<int> count;//count the frequencies of the unique numbers
vector<char> real_num;//record the unique numbers in the string
int temp_count = 0;
for(int i = 0; i < s.length(); i ++){
if(i==0 || s[i] == s[i-1]) temp_count ++;//if the same or it is the head of the string, count + 1
else{
count.push_back(temp_count);//if not the same, we are done with this number.record its count
real_num.push_back(s[i-1]);//record this number
temp_count = 1;
}
}
count.push_back(temp_count);
real_num.push_back(s[s.length() - 1]);
string res = "";
for(int i = 0; i < count.size(); i++){
res += '0' + count[i];//say count first
res += real_num[i];//say the number
}
return count_and_say(res,n-1);
}
};
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.
Solution:
for 1 to n:
Set current string = "";
Given a string, count the frequency of every unique number. Then append the (frequency in string) + (the number in string) to current string.
Code:
class Solution {
public:
string countAndSay(int n) {
string s = "1";
return count_and_say(s,n);
}
string count_and_say(string s, int n){
if(n == 1) return s;
vector<int> count;//count the frequencies of the unique numbers
vector<char> real_num;//record the unique numbers in the string
int temp_count = 0;
for(int i = 0; i < s.length(); i ++){
if(i==0 || s[i] == s[i-1]) temp_count ++;//if the same or it is the head of the string, count + 1
else{
count.push_back(temp_count);//if not the same, we are done with this number.record its count
real_num.push_back(s[i-1]);//record this number
temp_count = 1;
}
}
count.push_back(temp_count);
real_num.push_back(s[s.length() - 1]);
string res = "";
for(int i = 0; i < count.size(); i++){
res += '0' + count[i];//say count first
res += real_num[i];//say the number
}
return count_and_say(res,n-1);
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解