Skip to content

Commit 45c0727

Browse files
committed
feat: add solutions to lc problem: No.0481
No.0481.Magical String
1 parent 1bc6576 commit 45c0727

File tree

6 files changed

+136
-139
lines changed

6 files changed

+136
-139
lines changed

solution/0400-0499/0481.Magical String/README.md

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,37 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
**方法一:模拟**
48+
**方法一:模拟构造过程**
4949

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$。
5179

5280
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
5381

@@ -60,15 +88,15 @@
6088
```python
6189
class Solution:
6290
def magicalString(self, n: int) -> int:
63-
s = list('1221121')
64-
i = 5
91+
s = [1, 2, 2]
92+
i = 2
6593
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]
7098
i += 1
71-
return s[:n].count('1')
99+
return s[:n].count(1)
72100
```
73101

74102
### **Java**
@@ -78,20 +106,19 @@ class Solution:
78106
```java
79107
class Solution {
80108
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);
89116
}
90117
++i;
91118
}
92119
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) {
95122
++ans;
96123
}
97124
}
@@ -106,47 +133,41 @@ class Solution {
106133
class Solution {
107134
public:
108135
int magicalString(int n) {
109-
string s = "1221121";
110-
int i = 5;
136+
vector<int> s = {1, 2, 2};
137+
int i = 2;
111138
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);
116143
}
117144
++i;
118145
}
119-
return count(s.begin(), s.begin() + n, '1');
146+
return count(s.begin(), s.begin() + n, 1);
120147
}
121148
};
122149
```
123150
124151
### **Go**
125152
126153
```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
130157
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)
146162
}
147163
i++
148164
}
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
150171
}
151172
```
152173

solution/0400-0499/0481.Magical String/README_EN.md

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,34 @@
4646
```python
4747
class Solution:
4848
def magicalString(self, n: int) -> int:
49-
s = list('1221121')
50-
i = 5
49+
s = [1, 2, 2]
50+
i = 2
5151
while len(s) < n:
52-
if s[i] == '1':
53-
s.append('2' if s[-1] == '1' else '1')
54-
else:
55-
s.extend(list('22' if s[-1] == '1' else '11'))
52+
pre = s[-1]
53+
cur = 3 - pre
54+
s += [cur] * s[i]
5655
i += 1
57-
return s[:n].count('1')
56+
return s[:n].count(1)
5857
```
5958

6059
### **Java**
6160

6261
```java
6362
class Solution {
6463
public int magicalString(int n) {
65-
StringBuilder s = new StringBuilder("1221121");
66-
int i = 5;
67-
while (s.length() < n) {
68-
char c = s.charAt(s.length() - 1);
69-
if (s.charAt(i) == '1') {
70-
s.append(c == '1' ? '2' : '1');
71-
} else {
72-
s.append(c == '1' ? "22" : "11");
64+
List<Integer> s = new ArrayList<>(Arrays.asList(1, 2, 2));
65+
int i = 2;
66+
while (s.size() < n) {
67+
int pre = s.get(s.size() - 1);
68+
int cur = 3 - pre;
69+
for (int j = 0; j < s.get(i); ++j) {
70+
s.add(cur);
7371
}
7472
++i;
7573
}
7674
int ans = 0;
77-
for (i = 0; i < n; ++i) {
78-
if (s.charAt(i) == '1') {
75+
for (int j = 0; j < n; ++j) {
76+
if (s.get(j) == 1) {
7977
++ans;
8078
}
8179
}
@@ -90,47 +88,41 @@ class Solution {
9088
class Solution {
9189
public:
9290
int magicalString(int n) {
93-
string s = "1221121";
94-
int i = 5;
91+
vector<int> s = {1, 2, 2};
92+
int i = 2;
9593
while (s.size() < n) {
96-
if (s[i] == '1') {
97-
s += s.back() == '1' ? "2" : "1";
98-
} else {
99-
s += s.back() == '1' ? "22" : "11";
94+
int pre = s.back();
95+
int cur = 3 - pre;
96+
for (int j = 0; j < s[i]; ++j) {
97+
s.emplace_back(cur);
10098
}
10199
++i;
102100
}
103-
return count(s.begin(), s.begin() + n, '1');
101+
return count(s.begin(), s.begin() + n, 1);
104102
}
105103
};
106104
```
107105
108106
### **Go**
109107
110108
```go
111-
func magicalString(n int) int {
112-
s := []byte("1221121")
113-
i := 5
109+
func magicalString(n int) (ans int) {
110+
s := []int{1, 2, 2}
111+
i := 2
114112
for len(s) < n {
115-
c := s[len(s)-1]
116-
if s[i] == '1' {
117-
if c == '1' {
118-
s = append(s, '2')
119-
} else {
120-
s = append(s, '1')
121-
}
122-
} else {
123-
if c == '1' {
124-
s = append(s, '2')
125-
s = append(s, '2')
126-
} else {
127-
s = append(s, '1')
128-
s = append(s, '1')
129-
}
113+
pre := s[len(s)-1]
114+
cur := 3 - pre
115+
for j := 0; j < s[i]; j++ {
116+
s = append(s, cur)
130117
}
131118
i++
132119
}
133-
return bytes.Count(s[:n], []byte("1"))
120+
for j := 0; j < n; j++ {
121+
if s[j] == 1 {
122+
ans++
123+
}
124+
}
125+
return
134126
}
135127
```
136128

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public:
33
int magicalString(int n) {
4-
string s = "1221121";
5-
int i = 5;
4+
vector<int> s = {1, 2, 2};
5+
int i = 2;
66
while (s.size() < n) {
7-
if (s[i] == '1') {
8-
s += s.back() == '1' ? "2" : "1";
9-
} else {
10-
s += s.back() == '1' ? "22" : "11";
7+
int pre = s.back();
8+
int cur = 3 - pre;
9+
for (int j = 0; j < s[i]; ++j) {
10+
s.emplace_back(cur);
1111
}
1212
++i;
1313
}
14-
return count(s.begin(), s.begin() + n, '1');
14+
return count(s.begin(), s.begin() + n, 1);
1515
}
1616
};
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
func magicalString(n int) int {
2-
s := []byte("1221121")
3-
i := 5
1+
func magicalString(n int) (ans int) {
2+
s := []int{1, 2, 2}
3+
i := 2
44
for len(s) < n {
5-
c := s[len(s)-1]
6-
if s[i] == '1' {
7-
if c == '1' {
8-
s = append(s, '2')
9-
} else {
10-
s = append(s, '1')
11-
}
12-
} else {
13-
if c == '1' {
14-
s = append(s, '2')
15-
s = append(s, '2')
16-
} else {
17-
s = append(s, '1')
18-
s = append(s, '1')
19-
}
5+
pre := s[len(s)-1]
6+
cur := 3 - pre
7+
for j := 0; j < s[i]; j++ {
8+
s = append(s, cur)
209
}
2110
i++
2211
}
23-
return bytes.Count(s[:n], []byte("1"))
12+
for j := 0; j < n; j++ {
13+
if s[j] == 1 {
14+
ans++
15+
}
16+
}
17+
return
2418
}

solution/0400-0499/0481.Magical String/Solution.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
class Solution {
22
public int magicalString(int n) {
3-
StringBuilder s = new StringBuilder("1221121");
4-
int i = 5;
5-
while (s.length() < n) {
6-
char c = s.charAt(s.length() - 1);
7-
if (s.charAt(i) == '1') {
8-
if (c == '1') {
9-
s.append('2');
10-
} else {
11-
s.append('1');
12-
}
13-
} else {
14-
if (c == '1') {
15-
s.append("22");
16-
} else {
17-
s.append("11");
18-
}
3+
List<Integer> s = new ArrayList<>(Arrays.asList(1, 2, 2));
4+
int i = 2;
5+
while (s.size() < n) {
6+
int pre = s.get(s.size() - 1);
7+
int cur = 3 - pre;
8+
for (int j = 0; j < s.get(i); ++j) {
9+
s.add(cur);
1910
}
2011
++i;
2112
}
2213
int ans = 0;
23-
for (i = 0; i < n; ++i) {
24-
if (s.charAt(i) == '1') {
14+
for (int j = 0; j < n; ++j) {
15+
if (s.get(j) == 1) {
2516
++ans;
2617
}
2718
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution:
22
def magicalString(self, n: int) -> int:
3-
s = list('1221121')
4-
i = 5
3+
s = [1, 2, 2]
4+
i = 2
55
while len(s) < n:
6-
if s[i] == '1':
7-
s.append('2' if s[-1] == '1' else '1')
8-
else:
9-
s.extend(list('22' if s[-1] == '1' else '11'))
6+
pre = s[-1]
7+
cur = 3 - pre
8+
s += [cur] * s[i]
109
i += 1
11-
return s[:n].count('1')
10+
return s[:n].count(1)

0 commit comments

Comments
 (0)