Skip to content

Commit e273882

Browse files
committed
feat: update solutions to lc problem: No.1888. Minimum Number of Flips to Make the Binary String Alternating
1 parent 5db27a4 commit e273882

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

solution/0300-0399/0328.Odd Even Linked List/Solution.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* Next *ListNode
66
* }
77
*/
8-
func oddEvenList(head *ListNode) *ListNode {
9-
if head == nil {
10-
return head
11-
}
12-
odd, even := head, head.Next
13-
evenHead := even
14-
for even != nil && even.Next != nil {
15-
odd.Next = even.Next
16-
odd = odd.Next
17-
even.Next = odd.Next
18-
even = even.Next
19-
}
20-
odd.Next = evenHead
21-
return head
8+
func oddEvenList(head *ListNode) *ListNode {
9+
if head == nil {
10+
return head
11+
}
12+
odd, even := head, head.Next
13+
evenHead := even
14+
for even != nil && even.Next != nil {
15+
odd.Next = even.Next
16+
odd = odd.Next
17+
even.Next = odd.Next
18+
even = even.Next
19+
}
20+
odd.Next = evenHead
21+
return head
2222
}

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
双倍字符串 + 滑动窗口”实现。
61+
“滑动窗口”实现。
6262

6363
<!-- tabs:start -->
6464

@@ -75,10 +75,9 @@ class Solution:
7575
for i, c in enumerate(s):
7676
cnt += c != target[i & 1]
7777
res = min(cnt, n - cnt)
78-
s *= 2
7978
for i in range(n):
8079
cnt -= s[i] != target[i & 1]
81-
cnt += s[i + n] != target[(i + n) & 1]
80+
cnt += s[i] != target[(i + n) & 1]
8281
res = min(res, cnt, n - cnt)
8382
return res
8483
```
@@ -97,10 +96,9 @@ class Solution {
9796
cnt += (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
9897
}
9998
int res = Math.min(cnt, n - cnt);
100-
s += s;
10199
for (int i = 0; i < n; ++i) {
102100
cnt -= (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
103-
cnt += (s.charAt(i + n) == target.charAt((i + n) & 1) ? 0 : 1);
101+
cnt += (s.charAt(i) == target.charAt((i + n) & 1) ? 0 : 1);
104102
res = Math.min(res, Math.min(cnt, n - cnt));
105103
}
106104
return res;

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/README_EN.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ class Solution:
6969
for i, c in enumerate(s):
7070
cnt += c != target[i & 1]
7171
res = min(cnt, n - cnt)
72-
s *= 2
7372
for i in range(n):
7473
cnt -= s[i] != target[i & 1]
75-
cnt += s[i + n] != target[(i + n) & 1]
74+
cnt += s[i] != target[(i + n) & 1]
7675
res = min(res, cnt, n - cnt)
7776
return res
7877
```
@@ -89,10 +88,9 @@ class Solution {
8988
cnt += (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
9089
}
9190
int res = Math.min(cnt, n - cnt);
92-
s += s;
9391
for (int i = 0; i < n; ++i) {
9492
cnt -= (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
95-
cnt += (s.charAt(i + n) == target.charAt((i + n) & 1) ? 0 : 1);
93+
cnt += (s.charAt(i) == target.charAt((i + n) & 1) ? 0 : 1);
9694
res = Math.min(res, Math.min(cnt, n - cnt));
9795
}
9896
return res;

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ public int minFlips(String s) {
77
cnt += (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
88
}
99
int res = Math.min(cnt, n - cnt);
10-
s += s;
1110
for (int i = 0; i < n; ++i) {
1211
cnt -= (s.charAt(i) == target.charAt(i & 1) ? 0 : 1);
13-
cnt += (s.charAt(i + n) == target.charAt((i + n) & 1) ? 0 : 1);
12+
cnt += (s.charAt(i) == target.charAt((i + n) & 1) ? 0 : 1);
1413
res = Math.min(res, Math.min(cnt, n - cnt));
1514
}
1615
return res;

solution/1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ def minFlips(self, s: str) -> int:
66
for i, c in enumerate(s):
77
cnt += c != target[i & 1]
88
res = min(cnt, n - cnt)
9-
s *= 2
109
for i in range(n):
1110
cnt -= s[i] != target[i & 1]
12-
cnt += s[i + n] != target[(i + n) & 1]
11+
cnt += s[i] != target[(i + n) & 1]
1312
res = min(res, cnt, n - cnt)
1413
return res

0 commit comments

Comments
 (0)