1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
struct Solution;
impl Solution {
fn magical_string(n: i32) -> i32 {
if n == 0 {
return 0;
}
if n <= 3 {
return 1;
}
let n = n as usize;
let mut a = vec![1, 2, 2];
let mut i = 2;
let mut x = 1;
let mut res = 1;
loop {
for _ in 0..a[i] {
if x == 1 {
res += 1;
}
a.push(x);
if a.len() >= n {
return res;
}
}
x = 3 - x;
i += 1;
}
}
}
#[test]
fn test() {
let n = 6;
let res = 3;
assert_eq!(Solution::magical_string(n), res);
}