[leetcode-38]Count and Say
2017-01-09 12:04
381 查看
自动生成序列的一道题,想起来昨天比赛里遇见的另一道题,问C_∞序列第N位是多少,昨天死磕到最后也就是写了个垃圾代码,然后输入N>10就不能运行,觉得这类题的要点是要开两个序列,一个生成另一个后,然后拷贝回来,然后记得往后推测的两个条件,
1.注意现有数列边界,不要越界,例如下面代码中的i+1<res.size()
2.注意题目限制推到第X位,例如Count and Say里要推到第N个序列,就有一个while(n--)的限制,昨天的垃圾代码里就有一个i<n的限制
然后上代码,先上Count and Say标程(题目没看懂直接看了标程...):
string countAndSay(int n) {
if (n == 0) return "";
string res = "1";
while (--n) {
string cur = "";
for (int i = 0; i < res.size(); i++) {
int count = 1;
while ((i + 1 < res.size()) && (res[i] == res[i + 1])){
count++;
i++;
}
cur += to_string(count) + res[i];
}
res = cur;
}
return res;
}然后是昨天的题目[leetcode-481]Magic String:
A magical string S consists of only '1' and '2' and obeys the following rules:
The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.
The first few elements of string S is the following: S = "1221121221221121122……"
If we group the consecutive '1's and '2's in S, it will be:
1 22 11 2 1 22 1 22 11 2 11 22 ......
and the occurrences of '1's or '2's in each group are:
1 2 2 1 1 2 1 2 2 1 2 2 ......
You can see that the occurrence sequence above is the S itself.
Given an integer N as input, return the number of '1's in the first N number in the magical string S.
Note: N will not exceed 100,000.附垃圾代码供参考,题解稍后我改改再发:
int magicalString(int n) {
int s[3125],k=2,countx=1,j,i,flag=0;
//k代表递推出来的位置
//countx代表第k位前1的个数
//i,j代表正在从(32*j+i)递推
s[0]=1;
for(i=2,j=0;k<n;i++){
if(i==32){
i=0;j++;
}
if(s[j]&1<<i==1){ //当前连续序列长度为1时
if(flag==1) //当前序列当1时
{
s[k/32]+=1<<k%32;
countx+=1;
}
}
else{ //当前连续序列长度为2时
if(flag==1) //当前序列应当添1时
{
s[k/32]+=1<<k%32;
k++;
s[k/32]+=1<<k%32;
countx+=2;
}
else{
k+=1;
}
}
k+=1;
flag=1-flag;
}
cout<<s[0]<<endl;
return countx;
}
1.注意现有数列边界,不要越界,例如下面代码中的i+1<res.size()
2.注意题目限制推到第X位,例如Count and Say里要推到第N个序列,就有一个while(n--)的限制,昨天的垃圾代码里就有一个i<n的限制
然后上代码,先上Count and Say标程(题目没看懂直接看了标程...):
string countAndSay(int n) {
if (n == 0) return "";
string res = "1";
while (--n) {
string cur = "";
for (int i = 0; i < res.size(); i++) {
int count = 1;
while ((i + 1 < res.size()) && (res[i] == res[i + 1])){
count++;
i++;
}
cur += to_string(count) + res[i];
}
res = cur;
}
return res;
}然后是昨天的题目[leetcode-481]Magic String:
A magical string S consists of only '1' and '2' and obeys the following rules:
The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.
The first few elements of string S is the following: S = "1221121221221121122……"
If we group the consecutive '1's and '2's in S, it will be:
1 22 11 2 1 22 1 22 11 2 11 22 ......
and the occurrences of '1's or '2's in each group are:
1 2 2 1 1 2 1 2 2 1 2 2 ......
You can see that the occurrence sequence above is the S itself.
Given an integer N as input, return the number of '1's in the first N number in the magical string S.
Note: N will not exceed 100,000.附垃圾代码供参考,题解稍后我改改再发:
int magicalString(int n) {
int s[3125],k=2,countx=1,j,i,flag=0;
//k代表递推出来的位置
//countx代表第k位前1的个数
//i,j代表正在从(32*j+i)递推
s[0]=1;
for(i=2,j=0;k<n;i++){
if(i==32){
i=0;j++;
}
if(s[j]&1<<i==1){ //当前连续序列长度为1时
if(flag==1) //当前序列当1时
{
s[k/32]+=1<<k%32;
countx+=1;
}
}
else{ //当前连续序列长度为2时
if(flag==1) //当前序列应当添1时
{
s[k/32]+=1<<k%32;
k++;
s[k/32]+=1<<k%32;
countx+=2;
}
else{
k+=1;
}
}
k+=1;
flag=1-flag;
}
cout<<s[0]<<endl;
return countx;
}
相关文章推荐
- Leetcode 38 Count and Say
- LeetCode38 Count and Say(C语言解决)
- leetcode_question_38 Count and Say
- LeetCode_38---Count and Say
- LeetCode 38 Count and Say(字符串生成)
- Leetcode 38 Count and Say 传说中的递推
- leetcode 38:count and say
- leetcode-38 Count and Say
- 【LeetCode】38 - Count and Say
- leetcode 38:Count and Say
- [Leetcode]_38 Count and Say
- LeetCode-38-Count and Say(计数并显示)
- LeetCode(38) Count and Say
- LeetCode 38:Count and Say
- leetcode-38-count and say
- leetcode-38-Count and Say 基础题
- leetcode 38: Count and Say
- [LeetCode]38 Count and Say
- Leetcode 38,Count and say
- LeetCode 38 : Count and Say (Java)