File tree Expand file tree Collapse file tree 5 files changed +83
-3
lines changed
solution/1700-1799/1758.Minimum Changes To Make Alternating Binary String Expand file tree Collapse file tree 5 files changed +83
-3
lines changed Original file line number Diff line number Diff line change 44
44
<li><code>s[i]</code> 是 <code>'0'</code> 或 <code>'1'</code></li>
45
45
</ul >
46
46
47
-
48
47
## 解法
49
48
50
49
<!-- 这里可写通用的实现逻辑 -->
56
55
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
56
58
57
``` python
59
-
58
+ class Solution :
59
+ def minOperations (self , s : str ) -> int :
60
+ cnt = 0
61
+ for i, c in enumerate (s):
62
+ cnt += c == ' 01' [i & 1 ]
63
+ return min (cnt, len (s) - cnt)
60
64
```
61
65
62
66
### ** Java**
63
67
64
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
69
66
70
``` java
71
+ class Solution {
72
+ public int minOperations (String s ) {
73
+ int cnt = 0 , n = s. length();
74
+ for (int i = 0 ; i < n; ++ i) {
75
+ cnt += (s. charAt(i) == " 01" . charAt(i & 1 ) ? 1 : 0 );
76
+ }
77
+ return Math . min(cnt, n - cnt);
78
+ }
79
+ }
80
+ ```
67
81
82
+ ### ** C++**
83
+
84
+ ``` cpp
85
+ class Solution {
86
+ public:
87
+ int minOperations(string s) {
88
+ int cnt = 0, n = s.size();
89
+ for (int i = 0; i < n; ++i) {
90
+ cnt += s[ i] == "01"[ i & 1] ;
91
+ }
92
+ return min(cnt, n - cnt);
93
+ }
94
+ };
68
95
```
69
96
70
97
### **...**
Original file line number Diff line number Diff line change 51
51
### ** Python3**
52
52
53
53
``` python
54
-
54
+ class Solution :
55
+ def minOperations (self , s : str ) -> int :
56
+ cnt = 0
57
+ for i, c in enumerate (s):
58
+ cnt += c == ' 01' [i & 1 ]
59
+ return min (cnt, len (s) - cnt)
55
60
```
56
61
57
62
### ** Java**
58
63
59
64
``` java
65
+ class Solution {
66
+ public int minOperations (String s ) {
67
+ int cnt = 0 , n = s. length();
68
+ for (int i = 0 ; i < n; ++ i) {
69
+ cnt += (s. charAt(i) == " 01" . charAt(i & 1 ) ? 1 : 0 );
70
+ }
71
+ return Math . min(cnt, n - cnt);
72
+ }
73
+ }
74
+ ```
60
75
76
+ ### ** C++**
77
+
78
+ ``` cpp
79
+ class Solution {
80
+ public:
81
+ int minOperations(string s) {
82
+ int cnt = 0, n = s.size();
83
+ for (int i = 0; i < n; ++i) {
84
+ cnt += s[ i] == "01"[ i & 1] ;
85
+ }
86
+ return min(cnt, n - cnt);
87
+ }
88
+ };
61
89
```
62
90
63
91
### **...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minOperations (string s) {
4
+ int cnt = 0 , n = s.size ();
5
+ for (int i = 0 ; i < n; ++i) {
6
+ cnt += s[i] == " 01" [i & 1 ];
7
+ }
8
+ return min (cnt, n - cnt);
9
+ }
10
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minOperations (String s ) {
3
+ int cnt = 0 , n = s .length ();
4
+ for (int i = 0 ; i < n ; ++i ) {
5
+ cnt += (s .charAt (i ) == "01" .charAt (i & 1 ) ? 1 : 0 );
6
+ }
7
+ return Math .min (cnt , n - cnt );
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minOperations (self , s : str ) -> int :
3
+ cnt = 0
4
+ for i , c in enumerate (s ):
5
+ cnt += c == '01' [i & 1 ]
6
+ return min (cnt , len (s ) - cnt )
You can’t perform that action at this time.
0 commit comments