File tree 5 files changed +21
-27
lines changed
0300-0399/0328.Odd Even Linked List
1800-1899/1888.Minimum Number of Flips to Make the Binary String Alternating
5 files changed +21
-27
lines changed Original file line number Diff line number Diff line change 5
5
* Next *ListNode
6
6
* }
7
7
*/
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
22
22
}
Original file line number Diff line number Diff line change 58
58
59
59
<!-- 这里可写通用的实现逻辑 -->
60
60
61
- “双倍字符串 + 滑动窗口”实现。
61
+ “滑动窗口”实现。
62
62
63
63
<!-- tabs:start -->
64
64
@@ -75,10 +75,9 @@ class Solution:
75
75
for i, c in enumerate (s):
76
76
cnt += c != target[i & 1 ]
77
77
res = min (cnt, n - cnt)
78
- s *= 2
79
78
for i in range (n):
80
79
cnt -= s[i] != target[i & 1 ]
81
- cnt += s[i + n ] != target[(i + n) & 1 ]
80
+ cnt += s[i] != target[(i + n) & 1 ]
82
81
res = min (res, cnt, n - cnt)
83
82
return res
84
83
```
@@ -97,10 +96,9 @@ class Solution {
97
96
cnt += (s. charAt(i) == target. charAt(i & 1 ) ? 0 : 1 );
98
97
}
99
98
int res = Math . min(cnt, n - cnt);
100
- s += s;
101
99
for (int i = 0 ; i < n; ++ i) {
102
100
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 );
104
102
res = Math . min(res, Math . min(cnt, n - cnt));
105
103
}
106
104
return res;
Original file line number Diff line number Diff line change @@ -69,10 +69,9 @@ class Solution:
69
69
for i, c in enumerate (s):
70
70
cnt += c != target[i & 1 ]
71
71
res = min (cnt, n - cnt)
72
- s *= 2
73
72
for i in range (n):
74
73
cnt -= s[i] != target[i & 1 ]
75
- cnt += s[i + n ] != target[(i + n) & 1 ]
74
+ cnt += s[i] != target[(i + n) & 1 ]
76
75
res = min (res, cnt, n - cnt)
77
76
return res
78
77
```
@@ -89,10 +88,9 @@ class Solution {
89
88
cnt += (s. charAt(i) == target. charAt(i & 1 ) ? 0 : 1 );
90
89
}
91
90
int res = Math . min(cnt, n - cnt);
92
- s += s;
93
91
for (int i = 0 ; i < n; ++ i) {
94
92
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 );
96
94
res = Math . min(res, Math . min(cnt, n - cnt));
97
95
}
98
96
return res;
Original file line number Diff line number Diff line change @@ -7,10 +7,9 @@ public int minFlips(String s) {
7
7
cnt += (s .charAt (i ) == target .charAt (i & 1 ) ? 0 : 1 );
8
8
}
9
9
int res = Math .min (cnt , n - cnt );
10
- s += s ;
11
10
for (int i = 0 ; i < n ; ++i ) {
12
11
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 );
14
13
res = Math .min (res , Math .min (cnt , n - cnt ));
15
14
}
16
15
return res ;
Original file line number Diff line number Diff line change @@ -6,9 +6,8 @@ def minFlips(self, s: str) -> int:
6
6
for i , c in enumerate (s ):
7
7
cnt += c != target [i & 1 ]
8
8
res = min (cnt , n - cnt )
9
- s *= 2
10
9
for i in range (n ):
11
10
cnt -= s [i ] != target [i & 1 ]
12
- cnt += s [i + n ] != target [(i + n ) & 1 ]
11
+ cnt += s [i ] != target [(i + n ) & 1 ]
13
12
res = min (res , cnt , n - cnt )
14
13
return res
You can’t perform that action at this time.
0 commit comments