File tree Expand file tree Collapse file tree 6 files changed +71
-8
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 6 files changed +71
-8
lines changed Original file line number Diff line number Diff line change @@ -13074,12 +13074,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
13074
13074
//|Easy
13075
13075
//|
13076
13076
13077
- // |{counter:codes}
13078
- // |{leetcode_base_url}/minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
13079
- // |{source_base_url}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.java[Java]
13080
- // |{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
13081
- // |Medium
13082
- // |
13077
+ |{counter:codes}
13078
+ |{leetcode_base_url}/minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
13079
+ |{source_base_url}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.java[Java]
13080
+ |{doc_base_url}/1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
13081
+ |Medium
13082
+ |
13083
13083
13084
13084
//|{counter:codes}
13085
13085
//|{leetcode_base_url}/finding-pairs-with-a-certain-sum/[1865. Finding Pairs With a Certain Sum^]
Original file line number Diff line number Diff line change @@ -249,6 +249,7 @@ image::images/quick-sort-01.gif[{image_attr}]
249
249
250
250
== 参考资料
251
251
252
+ . https://leetcode.cn/circle/discuss/RvFUtj/[如何科学刷题? - 按照难度系数刷题^]
252
253
. https://www.designgurus.io/course/grokking-the-coding-interview[Grokking the Coding Interview Patterns^]
253
254
. https://leetcode.ca/all/1650.html[Leetcode 1650. Lowest Common Ancestor of a Binary Tree III^] -- 会员题目可以在这里查找。
254
255
. https://github.com/doocs/leetcode[doocs/leetcode: 🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解^] -- 也可以在这里找题目
Original file line number Diff line number Diff line change @@ -41,14 +41,15 @@ The string is now alternating.
41
41
*Constraints:*
42
42
43
43
44
- * `1 <= s.length <= 1000`
44
+ * `1 \ <= s.length \ <= 1000`
45
45
* `s[i]` is either `'0'` or `'1'`.
46
46
47
47
48
48
49
49
50
50
== 思路分析
51
51
52
+ 如果是交替出现,那么只有 `010...` 或者 `101...` 这两种类型,统计各个比特位不正确的位数再除以 2 即可。(交换一次,可以解决两个不正确比特位)。
52
53
53
54
[[src-1864]]
54
55
[tabs]
@@ -75,4 +76,5 @@ include::{sourcedir}/_1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating.
75
76
76
77
== 参考资料
77
78
79
+ . https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/solutions/779087/minimum-number-of-swaps-to-make-the-bina-z0qy/[1864. 构成交替字符串需要的最小交换次数 - 官方题解^]
78
80
Original file line number Diff line number Diff line change @@ -3802,7 +3802,7 @@ include::1823-find-the-winner-of-the-circular-game.adoc[leveloffset=+1]
3802
3802
3803
3803
// include::1863-sum-of-all-subset-xor-totals.adoc[leveloffset=+1]
3804
3804
3805
- // include::1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[leveloffset=+1]
3805
+ include::1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[leveloffset=+1]
3806
3806
3807
3807
// include::1865-finding-pairs-with-a-certain-sum.adoc[leveloffset=+1]
3808
3808
Original file line number Diff line number Diff line change 853
853
|{doc_base_url} /0079-word-search.adoc[题解]
854
854
|✅ 回溯
855
855
856
+ |{counter:codes}
857
+ |{leetcode_base_url} /minimum-number-of-swaps-to-make-the-binary-string-alternating/[1864. Minimum Number of Swaps to Make the Binary String Alternating^]
858
+ |{doc_base_url} /1864-minimum-number-of-swaps-to-make-the-binary-string-alternating.adoc[题解]
859
+ |❌ 贪心算法
860
+
856
861
|===
857
862
858
863
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
1
+ package com .diguage .algo .leetcode ;
2
+
3
+ public class _1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating {
4
+ // tag::answer[]
5
+
6
+ /**
7
+ * @author D瓜哥 · https://www.diguage.com
8
+ * @since 2024-09-24 16:26:07
9
+ */
10
+ public int minSwaps (String s ) {
11
+ int [] bits = bits (s );
12
+ int b0 = bits [0 ];
13
+ int b1 = bits [1 ];
14
+ int n = s .length ();
15
+ int result = Integer .MAX_VALUE ;
16
+ // 1010...
17
+ if (b0 == n / 2 && b1 == (n + 1 ) / 2 ) {
18
+ int diff = 0 ;
19
+ for (int i = 0 ; i < n ; i ++) {
20
+ if (s .charAt (i ) - '0' == i % 2 ) {
21
+ diff ++;
22
+ }
23
+ }
24
+ result = Math .min (result , diff / 2 );
25
+ }
26
+ // 0101...
27
+ if (b0 == (n + 1 ) / 2 && b1 == n / 2 ) {
28
+ int diff = 0 ;
29
+ for (int i = 0 ; i < n ; i ++) {
30
+ if (s .charAt (i ) - '0' != i % 2 ) {
31
+ diff ++;
32
+ }
33
+ }
34
+ result = Math .min (result , diff / 2 );
35
+ }
36
+ return result != Integer .MAX_VALUE ? result : -1 ;
37
+ }
38
+
39
+ private int [] bits (String s ) {
40
+ int b0 = 0 ;
41
+ for (int i = 0 ; i < s .length (); i ++) {
42
+ if (s .charAt (i ) == '0' ) {
43
+ b0 ++;
44
+ }
45
+ }
46
+ return new int []{b0 , s .length () - b0 };
47
+ }
48
+
49
+ // end::answer[]
50
+
51
+ public static void main (String [] args ) {
52
+ new _1864_MinimumNumberOfSwapsToMakeTheBinaryStringAlternating ()
53
+ .minSwaps ("1110" );
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments