45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
- ** 方法一:模拟 **
48
+ ** 方法一:模拟构造过程 **
49
49
50
- 直接模拟字符串添加。
50
+ 根据题目,我们得知,字符串 $s$ 的每一组数字都可以由字符串 $s$ 自身的数字得到。
51
+
52
+ 字符串 $s$ 前两组数字为 $1$ 和 $22$,是由字符串 $s$ 的第一个数字 $1$ 和第二个数字 $2$ 得到的,并且第一组数字只包含 $1$,第二组数字只包含 $2$,第三组数字只包含 $1$,以此类推。
53
+
54
+ 由于前两组数字已知,我们初始化字符串 $s$ 为 $122$,然后从第三组开始构造,第三组数字是由字符串 $s$ 的第三个数字(下标 $i=2$)得到,因此我们此时将指针 $i$ 指向字符串 $s$ 的第三个数字 $2$。
55
+
56
+ ```
57
+ 1 2 2
58
+ ^
59
+ i
60
+ ```
61
+
62
+ 指向 $i$ 的数字为 $2$,表示第三组的数字会出现两次,并且,由于前一组数字为 $2$,组之间数字交替出现,因此第三组数字为两个 $1$,即 $11$。构造后,指针 $i$ 移动到下一个位置,即指向字符串 $s$ 的第四个数字 $1$。
63
+
64
+ ```
65
+ 1 2 2 1 1
66
+ ^
67
+ i
68
+ ```
69
+
70
+ 这时候指针 $i$ 指向的数字为 $1$,表示第四组的数字会出现一次,并且,由于前一组数字为 $1$,组之间数字交替出现,因此第四组数字为一个 $2$,即 $2$。构造后,指针 $i$ 移动到下一个位置,即指向字符串 $s$ 的第五个数字 $1$。
71
+
72
+ ```
73
+ 1 2 2 1 1 2
74
+ ^
75
+ i
76
+ ```
77
+
78
+ 我们按照这个规则,依次模拟构造过程,直到字符串 $s$ 的长度大于等于 $n$。
51
79
52
80
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
53
81
60
88
``` python
61
89
class Solution :
62
90
def magicalString (self , n : int ) -> int :
63
- s = list ( ' 1221121 ' )
64
- i = 5
91
+ s = [ 1 , 2 , 2 ]
92
+ i = 2
65
93
while len (s) < n:
66
- if s[i] == ' 1 ' :
67
- s.append( ' 2 ' if s[ - 1 ] == ' 1 ' else ' 1 ' )
68
- else :
69
- s.extend( list ( ' 22 ' if s[ - 1 ] == ' 1 ' else ' 11 ' ))
94
+ pre = s[ - 1 ]
95
+ cur = 3 - pre
96
+ # cur 表示这一组的数字,s[i] 表示这一组数字出现的次数
97
+ s += [cur] * s[i]
70
98
i += 1
71
- return s[:n].count(' 1 ' )
99
+ return s[:n].count(1 )
72
100
```
73
101
74
102
### ** Java**
@@ -78,20 +106,19 @@ class Solution:
78
106
``` java
79
107
class Solution {
80
108
public int magicalString (int n ) {
81
- StringBuilder s = new StringBuilder (" 1221121" );
82
- int i = 5 ;
83
- while (s. length() < n) {
84
- char c = s. charAt(s. length() - 1 );
85
- if (s. charAt(i) == ' 1' ) {
86
- s. append(c == ' 1' ? ' 2' : ' 1' );
87
- } else {
88
- s. append(c == ' 1' ? " 22" : " 11" );
109
+ List<Integer > s = new ArrayList<> (Arrays . asList(1 , 2 , 2 ));
110
+ int i = 2 ;
111
+ while (s. size() < n) {
112
+ int pre = s. get(s. size() - 1 );
113
+ int cur = 3 - pre;
114
+ for (int j = 0 ; j < s. get(i); ++ j) {
115
+ s. add(cur);
89
116
}
90
117
++ i;
91
118
}
92
119
int ans = 0 ;
93
- for (i = 0 ; i < n; ++ i ) {
94
- if (s. charAt(i ) == ' 1 ' ) {
120
+ for (int j = 0 ; j < n; ++ j ) {
121
+ if (s. get(j ) == 1 ) {
95
122
++ ans;
96
123
}
97
124
}
@@ -106,47 +133,41 @@ class Solution {
106
133
class Solution {
107
134
public:
108
135
int magicalString(int n) {
109
- string s = "1221121" ;
110
- int i = 5 ;
136
+ vector< int > s = {1, 2, 2} ;
137
+ int i = 2 ;
111
138
while (s.size() < n) {
112
- if (s [ i ] == '1') {
113
- s += s.back() == '1' ? "2" : "1" ;
114
- } else {
115
- s += s.back() == '1' ? "22" : "11" ;
139
+ int pre = s.back();
140
+ int cur = 3 - pre ;
141
+ for (int j = 0; j < s [ i ] ; ++j) {
142
+ s.emplace_back(cur) ;
116
143
}
117
144
++i;
118
145
}
119
- return count(s.begin(), s.begin() + n, '1' );
146
+ return count(s.begin(), s.begin() + n, 1 );
120
147
}
121
148
};
122
149
```
123
150
124
151
### **Go**
125
152
126
153
```go
127
- func magicalString(n int) int {
128
- s := []byte("1221121")
129
- i := 5
154
+ func magicalString(n int) (ans int) {
155
+ s := []int{1, 2, 2}
156
+ i := 2
130
157
for len(s) < n {
131
- c := s[len(s)-1]
132
- if s[i] == '1' {
133
- if c == '1' {
134
- s = append(s, '2')
135
- } else {
136
- s = append(s, '1')
137
- }
138
- } else {
139
- if c == '1' {
140
- s = append(s, '2')
141
- s = append(s, '2')
142
- } else {
143
- s = append(s, '1')
144
- s = append(s, '1')
145
- }
158
+ pre := s[len(s)-1]
159
+ cur := 3 - pre
160
+ for j := 0; j < s[i]; j++ {
161
+ s = append(s, cur)
146
162
}
147
163
i++
148
164
}
149
- return bytes.Count(s[:n], []byte("1"))
165
+ for j := 0; j < n; j++ {
166
+ if s[j] == 1 {
167
+ ans++
168
+ }
169
+ }
170
+ return
150
171
}
151
172
```
152
173
0 commit comments