File tree Expand file tree Collapse file tree 6 files changed +251
-2
lines changed
solution/0400-0499/0481.Magical String Expand file tree Collapse file tree 6 files changed +251
-2
lines changed Original file line number Diff line number Diff line change 45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
+ ** 方法一:模拟**
49
+
50
+ 直接模拟字符串添加。
51
+
52
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。
53
+
48
54
<!-- tabs:start -->
49
55
50
56
### ** Python3**
51
57
52
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
59
54
60
``` 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' )
56
72
```
57
73
58
74
### ** Java**
59
75
60
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
77
62
78
``` 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
+ ```
63
123
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
+ }
64
151
```
65
152
66
153
### ** ...**
Original file line number Diff line number Diff line change 44
44
### ** Python3**
45
45
46
46
``` 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' )
48
58
```
49
59
50
60
### ** Java**
51
61
52
62
``` 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
+ ```
53
107
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
+ }
54
135
```
55
136
56
137
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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' )
You can’t perform that action at this time.
0 commit comments