Skip to content

Commit 894ebe6

Browse files
authored
feat: add solutions to lc problems: No.0481,0482 (#3246)
* No.0481.Magical String * No.0482.License Key Formatting
1 parent 6beb6bd commit 894ebe6

File tree

13 files changed

+367
-218
lines changed

13 files changed

+367
-218
lines changed

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

+18-19
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Solution:
115115
```java
116116
class Solution {
117117
public int magicalString(int n) {
118-
List<Integer> s = new ArrayList<>(Arrays.asList(1, 2, 2));
118+
List<Integer> s = new ArrayList<>(List.of(1, 2, 2));
119119
for (int i = 2; s.size() < n; ++i) {
120120
int pre = s.get(s.size() - 1);
121121
int cur = 3 - pre;
@@ -178,17 +178,15 @@ func magicalString(n int) (ans int) {
178178

179179
```ts
180180
function magicalString(n: number): number {
181-
const cs = [...'1221121'];
182-
let i = 5;
183-
while (cs.length < n) {
184-
const c = cs[cs.length - 1];
185-
cs.push(c === '1' ? '2' : '1');
186-
if (cs[i] !== '1') {
187-
cs.push(c === '1' ? '2' : '1');
181+
const s: number[] = [1, 2, 2];
182+
for (let i = 2; s.length < n; ++i) {
183+
let pre = s[s.length - 1];
184+
let cur = 3 - pre;
185+
for (let j = 0; j < s[i]; ++j) {
186+
s.push(cur);
188187
}
189-
i++;
190188
}
191-
return cs.slice(0, n).reduce((r, c) => r + (c === '1' ? 1 : 0), 0);
189+
return s.slice(0, n).filter(x => x === 1).length;
192190
}
193191
```
194192

@@ -197,18 +195,19 @@ function magicalString(n: number): number {
197195
```rust
198196
impl Solution {
199197
pub fn magical_string(n: i32) -> i32 {
200-
let n = n as usize;
201-
let mut s = String::from("1221121");
202-
let mut i = 5;
203-
while s.len() < n {
204-
let c = s.as_bytes()[s.len() - 1];
205-
s.push(if c == b'1' { '2' } else { '1' });
206-
if s.as_bytes()[i] != b'1' {
207-
s.push(if c == b'1' { '2' } else { '1' });
198+
let mut s = vec![1, 2, 2];
199+
let mut i = 2;
200+
201+
while s.len() < n as usize {
202+
let pre = s[s.len() - 1];
203+
let cur = 3 - pre;
204+
for _ in 0..s[i] {
205+
s.push(cur);
208206
}
209207
i += 1;
210208
}
211-
s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32
209+
210+
s.iter().take(n as usize).filter(|&&x| x == 1).count() as i32
212211
}
213212
}
214213
```

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

+51-21
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,39 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Simulate the Construction Process
60+
61+
According to the problem, we know that each group of numbers in the string $s$ can be obtained from the digits of the string $s$ itself.
62+
63+
The first two groups of numbers in string $s$ are $1$ and $22$, which are obtained from the first and second digits of string $s$, respectively. Moreover, the first group of numbers contains only $1$, the second group contains only $2$, the third group contains only $1$, and so on.
64+
65+
Since the first two groups of numbers are known, we initialize string $s$ as $122$, and then start constructing from the third group. The third group of numbers is obtained from the third digit of string $s$ (index $i=2$), so at this point, we point the pointer $i$ to the third digit $2$ of string $s$.
66+
67+
```
68+
1 2 2
69+
^
70+
i
71+
```
72+
73+
The digit pointed by pointer $i$ is $2$, indicating that the third group of numbers will appear twice. Since the previous group of numbers is $2$, and the numbers alternate between groups, the third group of numbers is two $1$s, i.e., $11$. After construction, the pointer $i$ moves to the next position, pointing to the fourth digit $1$ of string $s$.
74+
75+
```
76+
1 2 2 1 1
77+
^
78+
i
79+
```
80+
81+
At this point, the digit pointed by pointer $i$ is $1$, indicating that the fourth group of numbers will appear once. Since the previous group of numbers is $1$, and the numbers alternate between groups, the fourth group of numbers is one $2$, i.e., $2$. After construction, the pointer $i$ moves to the next position, pointing to the fifth digit $1$ of string $s$.
82+
83+
```
84+
1 2 2 1 1 2
85+
^
86+
i
87+
```
88+
89+
Following this rule, we simulate the construction process sequentially until the length of string $s$ is greater than or equal to $n$.
90+
91+
The time complexity is $O(n)$, and the space complexity is $O(n)$.
6092

6193
<!-- tabs:start -->
6294

@@ -70,7 +102,6 @@ class Solution:
70102
while len(s) < n:
71103
pre = s[-1]
72104
cur = 3 - pre
73-
# cur 表示这一组的数字,s[i] 表示这一组数字出现的次数
74105
s += [cur] * s[i]
75106
i += 1
76107
return s[:n].count(1)
@@ -81,7 +112,7 @@ class Solution:
81112
```java
82113
class Solution {
83114
public int magicalString(int n) {
84-
List<Integer> s = new ArrayList<>(Arrays.asList(1, 2, 2));
115+
List<Integer> s = new ArrayList<>(List.of(1, 2, 2));
85116
for (int i = 2; s.size() < n; ++i) {
86117
int pre = s.get(s.size() - 1);
87118
int cur = 3 - pre;
@@ -144,17 +175,15 @@ func magicalString(n int) (ans int) {
144175

145176
```ts
146177
function magicalString(n: number): number {
147-
const cs = [...'1221121'];
148-
let i = 5;
149-
while (cs.length < n) {
150-
const c = cs[cs.length - 1];
151-
cs.push(c === '1' ? '2' : '1');
152-
if (cs[i] !== '1') {
153-
cs.push(c === '1' ? '2' : '1');
178+
const s: number[] = [1, 2, 2];
179+
for (let i = 2; s.length < n; ++i) {
180+
let pre = s[s.length - 1];
181+
let cur = 3 - pre;
182+
for (let j = 0; j < s[i]; ++j) {
183+
s.push(cur);
154184
}
155-
i++;
156185
}
157-
return cs.slice(0, n).reduce((r, c) => r + (c === '1' ? 1 : 0), 0);
186+
return s.slice(0, n).filter(x => x === 1).length;
158187
}
159188
```
160189

@@ -163,18 +192,19 @@ function magicalString(n: number): number {
163192
```rust
164193
impl Solution {
165194
pub fn magical_string(n: i32) -> i32 {
166-
let n = n as usize;
167-
let mut s = String::from("1221121");
168-
let mut i = 5;
169-
while s.len() < n {
170-
let c = s.as_bytes()[s.len() - 1];
171-
s.push(if c == b'1' { '2' } else { '1' });
172-
if s.as_bytes()[i] != b'1' {
173-
s.push(if c == b'1' { '2' } else { '1' });
195+
let mut s = vec![1, 2, 2];
196+
let mut i = 2;
197+
198+
while s.len() < n as usize {
199+
let pre = s[s.len() - 1];
200+
let cur = 3 - pre;
201+
for _ in 0..s[i] {
202+
s.push(cur);
174203
}
175204
i += 1;
176205
}
177-
s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32
206+
207+
s.iter().take(n as usize).filter(|&&x| x == 1).count() as i32
178208
}
179209
}
180210
```

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public int magicalString(int n) {
3-
List<Integer> s = new ArrayList<>(Arrays.asList(1, 2, 2));
3+
List<Integer> s = new ArrayList<>(List.of(1, 2, 2));
44
for (int i = 2; s.size() < n; ++i) {
55
int pre = s.get(s.size() - 1);
66
int cur = 3 - pre;

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

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ def magicalString(self, n: int) -> int:
55
while len(s) < n:
66
pre = s[-1]
77
cur = 3 - pre
8-
# cur 表示这一组的数字,s[i] 表示这一组数字出现的次数
98
s += [cur] * s[i]
109
i += 1
1110
return s[:n].count(1)
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
impl Solution {
22
pub fn magical_string(n: i32) -> i32 {
3-
let n = n as usize;
4-
let mut s = String::from("1221121");
5-
let mut i = 5;
6-
while s.len() < n {
7-
let c = s.as_bytes()[s.len() - 1];
8-
s.push(if c == b'1' { '2' } else { '1' });
9-
if s.as_bytes()[i] != b'1' {
10-
s.push(if c == b'1' { '2' } else { '1' });
3+
let mut s = vec![1, 2, 2];
4+
let mut i = 2;
5+
6+
while s.len() < n as usize {
7+
let pre = s[s.len() - 1];
8+
let cur = 3 - pre;
9+
for _ in 0..s[i] {
10+
s.push(cur);
1111
}
1212
i += 1;
1313
}
14-
s.as_bytes()[0..n].iter().filter(|&v| v == &b'1').count() as i32
14+
15+
s.iter().take(n as usize).filter(|&&x| x == 1).count() as i32
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
function magicalString(n: number): number {
2-
const cs = [...'1221121'];
3-
let i = 5;
4-
while (cs.length < n) {
5-
const c = cs[cs.length - 1];
6-
cs.push(c === '1' ? '2' : '1');
7-
if (cs[i] !== '1') {
8-
cs.push(c === '1' ? '2' : '1');
2+
const s: number[] = [1, 2, 2];
3+
for (let i = 2; s.length < n; ++i) {
4+
let pre = s[s.length - 1];
5+
let cur = 3 - pre;
6+
for (let j = 0; j < s[i]; ++j) {
7+
s.push(cur);
98
}
10-
i++;
119
}
12-
return cs.slice(0, n).reduce((r, c) => r + (c === '1' ? 1 : 0), 0);
10+
return s.slice(0, n).filter(x => x === 1).length;
1311
}

0 commit comments

Comments
 (0)