Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ab2ddaf

Browse files
committed
feat: add solutions to lc problem: No.0481
No.0481.Magical String
1 parent afd770d commit ab2ddaf

File tree

6 files changed

+251
-2
lines changed

6 files changed

+251
-2
lines changed

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

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,109 @@
4545

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

48+
**方法一:模拟**
49+
50+
直接模拟字符串添加。
51+
52+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
5157

5258
<!-- 这里可写当前语言的特殊实现逻辑 -->
5359

5460
```python
55-
61+
class Solution:
62+
def magicalString(self, n: int) -> int:
63+
s = list('1221121')
64+
i = 5
65+
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'))
70+
i += 1
71+
return s[:n].count('1')
5672
```
5773

5874
### **Java**
5975

6076
<!-- 这里可写当前语言的特殊实现逻辑 -->
6177

6278
```java
79+
class Solution {
80+
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");
89+
}
90+
++i;
91+
}
92+
int ans = 0;
93+
for (i = 0; i < n; ++i) {
94+
if (s.charAt(i) == '1') {
95+
++ans;
96+
}
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int magicalString(int n) {
109+
string s = "1221121";
110+
int i = 5;
111+
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";
116+
}
117+
++i;
118+
}
119+
return count(s.begin(), s.begin() + n, '1');
120+
}
121+
};
122+
```
63123
124+
### **Go**
125+
126+
```go
127+
func magicalString(n int) int {
128+
s := []byte("1221121")
129+
i := 5
130+
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+
}
146+
}
147+
i++
148+
}
149+
return bytes.Count(s[:n], []byte("1"))
150+
}
64151
```
65152

66153
### **...**

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,94 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def magicalString(self, n: int) -> int:
49+
s = list('1221121')
50+
i = 5
51+
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'))
56+
i += 1
57+
return s[:n].count('1')
4858
```
4959

5060
### **Java**
5161

5262
```java
63+
class Solution {
64+
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");
73+
}
74+
++i;
75+
}
76+
int ans = 0;
77+
for (i = 0; i < n; ++i) {
78+
if (s.charAt(i) == '1') {
79+
++ans;
80+
}
81+
}
82+
return ans;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int magicalString(int n) {
93+
string s = "1221121";
94+
int i = 5;
95+
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";
100+
}
101+
++i;
102+
}
103+
return count(s.begin(), s.begin() + n, '1');
104+
}
105+
};
106+
```
53107
108+
### **Go**
109+
110+
```go
111+
func magicalString(n int) int {
112+
s := []byte("1221121")
113+
i := 5
114+
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+
}
130+
}
131+
i++
132+
}
133+
return bytes.Count(s[:n], []byte("1"))
134+
}
54135
```
55136

56137
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int magicalString(int n) {
4+
string s = "1221121";
5+
int i = 5;
6+
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";
11+
}
12+
++i;
13+
}
14+
return count(s.begin(), s.begin() + n, '1');
15+
}
16+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func magicalString(n int) int {
2+
s := []byte("1221121")
3+
i := 5
4+
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+
}
20+
}
21+
i++
22+
}
23+
return bytes.Count(s[:n], []byte("1"))
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
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+
}
19+
}
20+
++i;
21+
}
22+
int ans = 0;
23+
for (i = 0; i < n; ++i) {
24+
if (s.charAt(i) == '1') {
25+
++ans;
26+
}
27+
}
28+
return ans;
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def magicalString(self, n: int) -> int:
3+
s = list('1221121')
4+
i = 5
5+
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'))
10+
i += 1
11+
return s[:n].count('1')

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /