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.
Example 1:
Input: 6
Output: 3
Explanation: The first 6 elements of magical string S is "12211" and it contains three 1's, so return 3.
思路:
给定1和2组成的字符串,1和2描述了本身1或者2连续出现的次数。给定一个数字N,返回这种字符串前N个数中1的个数。
分析:
1 2 2 1 1 2 1 2 2 1 2 2……
1 22 11 2 1 22 1 22 11 2 11 22 ……
比如122就确定了1+2+2=5个数除去自身就是后面两个数,可以知道的是,后面两个数是2个1或者两个2.比如11,那么就再影响了了后面两个数不连续。比如22就影响了后面的四个数,可能出现 : 1122 1111 2222 2211
所以我们知道两个事:第一个:表示出现个数的字符串比较短,能决定后面的。第二个:这种决定是不一定的(有多种可能)。
那么有多种可能怎么办呢?我们继续回到题目给出的字符串:发现1,2是间隔的。所以,这个字符串在开头确定的前提下就已经被确定了。
所以我们动态的构造它直到N,在过程中计数。
再来分析规律:比如122后面为什么是11:
1、第三个数2决定了是两个数。
2、第三个数是2决定了该是1了。
归纳一般规律:我们需要知道探究对象前面的是1还是2,还需要知道对应应该添加几个。
class Solution {
public int magicalString(int n) {
StringBuilder sb = new StringBuilder("122");
int count = 1;
int slow = 2; //应该指示接下来
int fast = 2; //应该指示当前最末
while(sb.length() < n){
if(sb.charAt(slow) == '1'){ //表示接下来的是1个
if(sb.charAt(fast) == '1') //表示当前是1
sb.append(2); //那么就应该添加2
else if(sb.charAt(fast) == '2'){
sb.append(1);
count++;
}
fast++;
}else if(sb.charAt(slow) == '2'){ //表示接下来的是2个
if(sb.charAt(fast) == '1')
sb.append(22);
else if(sb.charAt(fast) == '2'){
sb.append(11);
count += 2;
}
fast += 2;
}
slow++;
}
if(sb.length() > n && sb.charAt(n) == '1') //最后一次如果是append(11),那么有可能多出一个1
count--;
return count;
}
}