From b6f09255e836e58c101526f940e69c4279b0ef6f Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年2月14日 22:13:52 +0700 Subject: [PATCH 1/3] Added tasks 922, 923, 924, 925, 926 --- .../Solution.java | 24 ++++++ .../s0922_sort_array_by_parity_ii/readme.md | 32 ++++++++ .../Solution.java | 31 ++++++++ .../s0923_3sum_with_multiplicity/readme.md | 41 ++++++++++ .../Solution.java | 74 +++++++++++++++++++ .../s0924_minimize_malware_spread/readme.md | 43 +++++++++++ .../s0925_long_pressed_name/Solution.java | 42 +++++++++++ .../s0925_long_pressed_name/readme.md | 28 +++++++ .../Solution.java | 19 +++++ .../readme.md | 38 ++++++++++ .../SolutionTest.java | 20 +++++ .../SolutionTest.java | 20 +++++ .../SolutionTest.java | 35 +++++++++ .../s0925_long_pressed_name/SolutionTest.java | 18 +++++ .../SolutionTest.java | 23 ++++++ 15 files changed, 488 insertions(+) create mode 100644 src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java create mode 100644 src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md create mode 100644 src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java create mode 100644 src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md create mode 100644 src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java create mode 100644 src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md create mode 100644 src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java create mode 100644 src/main/java/g0901_1000/s0925_long_pressed_name/readme.md create mode 100644 src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java create mode 100644 src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md create mode 100644 src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java create mode 100644 src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java create mode 100644 src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java create mode 100644 src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java create mode 100644 src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java diff --git a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java new file mode 100644 index 000000000..5786b6f9d --- /dev/null +++ b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java @@ -0,0 +1,24 @@ +package g0901_1000.s0922_sort_array_by_parity_ii; + +// #Easy #Array #Sorting #Two_Pointers + +public class Solution { + public int[] sortArrayByParityII(int[] nums) { + for (int i = 0, j = 1; i < nums.length - 1 && j < nums.length; ) { + if (nums[i] % 2 != 0 && nums[j] % 2 == 0) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + i += 2; + j += 2; + } + while (i < nums.length - 1 && nums[i] % 2 == 0) { + i += 2; + } + while (j < nums.length && nums[j] % 2 != 0) { + j += 2; + } + } + return nums; + } +} diff --git a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md new file mode 100644 index 000000000..d8291c7f7 --- /dev/null +++ b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md @@ -0,0 +1,32 @@ +922\. Sort Array By Parity II + +Easy + +Given an array of integers `nums`, half of the integers in `nums` are **odd**, and the other half are **even**. + +Sort the array so that whenever `nums[i]` is odd, `i` is **odd**, and whenever `nums[i]` is even, `i` is **even**. + +Return _any answer array that satisfies this condition_. + +**Example 1:** + +**Input:** nums = [4,2,5,7] + +**Output:** [4,5,2,7] + +**Explanation:** [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. + +**Example 2:** + +**Input:** nums = [2,3] + +**Output:** [2,3] + +**Constraints:** + +* 2 <= nums.length <= 2 * 104 +* `nums.length` is even. +* Half of the integers in `nums` are even. +* `0 <= nums[i] <= 1000` + +**Follow Up:** Could you solve it in-place? \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java new file mode 100644 index 000000000..8c340a78d --- /dev/null +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java @@ -0,0 +1,31 @@ +package g0901_1000.s0923_3sum_with_multiplicity; + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + private static final int MAX = 100; + + public int threeSumMulti(int[] arr, int target) { + int answer = 0; + int[] count_right = new int[MAX + 1]; + for (int num : arr) { + ++count_right[num]; + } + int[] count_left = new int[MAX + 1]; + for (int j = 0; j < arr.length - 1; j++) { + --count_right[arr[j]]; + int remains = target - arr[j]; + if (remains <= 2 * MAX) { + for (int v = 0; v <= Math.min(remains, MAX); v++) { + if (remains - v <= MAX) { + int count = count_right[v] * count_left[remains - v]; + if (count> 0) { + answer = (answer + count) % MOD; + } + } + } + } + ++count_left[arr[j]]; + } + return answer; + } +} diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md new file mode 100644 index 000000000..d43cf7ee0 --- /dev/null +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md @@ -0,0 +1,41 @@ +923\. 3Sum With Multiplicity + +Medium + +Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`. + +As the answer can be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** arr = [1,1,2,2,3,3,4,4,5,5], target = 8 + +**Output:** 20 + +**Explanation:** + + Enumerating by the values (arr[i], arr[j], arr[k]): + (1, 2, 5) occurs 8 times; + (1, 3, 4) occurs 8 times; + (2, 2, 4) occurs 2 times; + (2, 3, 3) occurs 2 times. + +**Example 2:** + +**Input:** arr = [1,1,2,2,2,2], target = 5 + +**Output:** 12 + +**Explanation:** + +arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times: + +We choose one 1 from [1,1] in 2 ways, + +and two 2s from [2,2,2,2] in 6 ways. + +**Constraints:** + +* `3 <= arr.length <= 3000` +* `0 <= arr[i] <= 100` +* `0 <= target <= 300` \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java new file mode 100644 index 000000000..1cd437b1f --- /dev/null +++ b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java @@ -0,0 +1,74 @@ +package g0901_1000.s0924_minimize_malware_spread; + +// #Hard #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find + +import java.util.HashMap; +import java.util.HashSet; + +public class Solution { + private int[][] par; + + public int minMalwareSpread(int[][] graph, int[] initial) { + int n = graph.length; + par = new int[n][2]; + for (int i = 0; i < par.length; i++) { + par[i][0] = i; + par[i][1] = 1; + } + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (graph[i][j] == 1) { + int li = find(i); + int lj = find(j); + if (li != lj) { + par[li][0] = lj; + par[lj][1] += par[li][1]; + } + } + } + } + HashMap> map = new HashMap(); + for (int val : initial) { + int lv = find(val); + if (!map.containsKey(lv)) { + map.put(lv, new HashSet()); + } + map.get(lv).add(val); + } + int ans = -1; + int max = Integer.MIN_VALUE; + + for (int val : initial) { + int lv = find(val); + if (map.get(lv).size() == 1) { + if (par[lv][1]> max) { + max = par[lv][1]; + ans = val; + } else if (par[lv][1] == max) { + if (val < ans) { + ans = val; + } + } + } else { + if (max < 0) { + max = 0; + ans = val; + } else if (max == 0) { + if (val < ans) { + ans = val; + } + } + } + } + + return ans; + } + + private int find(int x) { + if (par[x][0] == x) { + return x; + } + int temp = find(par[x][0]); + return par[x][0] = temp; + } +} diff --git a/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md b/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md new file mode 100644 index 000000000..a5f74d2ea --- /dev/null +++ b/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md @@ -0,0 +1,43 @@ +924\. Minimize Malware Spread + +Hard + +You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the ith node is directly connected to the jth node if `graph[i][j] == 1`. + +Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner. + +Suppose `M(initial)` is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove **exactly one node** from `initial`. + +Return the node that, if removed, would minimize `M(initial)`. If multiple nodes could be removed to minimize `M(initial)`, return such a node with **the smallest index**. + +Note that if a node was removed from the `initial` list of infected nodes, it might still be infected later due to the malware spread. + +**Example 1:** + +**Input:** graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] + +**Output:** 0 + +**Example 2:** + +**Input:** graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2] + +**Output:** 0 + +**Example 3:** + +**Input:** graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2] + +**Output:** 1 + +**Constraints:** + +* `n == graph.length` +* `n == graph[i].length` +* `2 <= n <= 300` +* `graph[i][j]` is `0` or `1`. +* `graph[i][j] == graph[j][i]` +* `graph[i][i] == 1` +* `1 <= initial.length <= n` +* `0 <= initial[i] <= n - 1` +* All the integers in `initial` are **unique**. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java b/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java new file mode 100644 index 000000000..472412501 --- /dev/null +++ b/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java @@ -0,0 +1,42 @@ +package g0901_1000.s0925_long_pressed_name; + +// #Easy #String #Two_Pointers + +public class Solution { + public boolean isLongPressedName(String name, String typed) { + int i = 0; + int j = 0; + char prev = '$'; + if (typed.length() < name.length()) { + return false; + } + + while (i < name.length() && j < typed.length()) { + while (j < typed.length() && typed.charAt(j) != name.charAt(i)) { + if (typed.charAt(j) != prev) { + return false; + } + if (j == typed.length() - 1) { + return false; + } + j++; + } + + prev = name.charAt(i); + i++; + j++; + } + + if (i < name.length()) { + return false; + } + + for (; j < typed.length(); j++) { + if (typed.charAt(j) != prev) { + return false; + } + } + + return true; + } +} diff --git a/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md b/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md new file mode 100644 index 000000000..b6900eefb --- /dev/null +++ b/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md @@ -0,0 +1,28 @@ +925\. Long Pressed Name + +Easy + +Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times. + +You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly none) being long pressed. + +**Example 1:** + +**Input:** name = "alex", typed = "aaleex" + +**Output:** true + +**Explanation:** 'a' and 'e' in 'alex' were long pressed. + +**Example 2:** + +**Input:** name = "saeed", typed = "ssaaedd" + +**Output:** false + +**Explanation:** 'e' must have been pressed twice, but it was not in the typed output. + +**Constraints:** + +* `1 <= name.length, typed.length <= 1000` +* `name` and `typed` consist of only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java new file mode 100644 index 000000000..882848fa9 --- /dev/null +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java @@ -0,0 +1,19 @@ +package g0901_1000.s0926_flip_string_to_monotone_increasing; + +class Solution { + public int minFlipsMonoIncr(String s) { + if (s == null || s.length() <= 1) return 0; + final int N = s.length(); + int countOnes = 0, countFlips = 0; + for (int i = 0; i < N; i++) { + if (s.charAt(i) == '0') { + if (countOnes == 0) continue; + else countFlips++; + } else { + countOnes++; + } + countFlips = Math.min(countFlips, countOnes); + } + return countFlips; + } +} diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md new file mode 100644 index 000000000..e1d18b25c --- /dev/null +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md @@ -0,0 +1,38 @@ +926\. Flip String to Monotone Increasing + +Medium + +A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none). + +You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`. + +Return _the minimum number of flips to make_ `s` _monotone increasing_. + +**Example 1:** + +**Input:** s = "00110" + +**Output:** 1 + +**Explanation:** We flip the last digit to get 00111. + +**Example 2:** + +**Input:** s = "010110" + +**Output:** 2 + +**Explanation:** We flip to get 011111, or alternatively 000111. + +**Example 3:** + +**Input:** s = "00011000" + +**Output:** 2 + +**Explanation:** We flip to get 00000000. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. \ No newline at end of file diff --git a/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java b/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java new file mode 100644 index 000000000..e792ffb0c --- /dev/null +++ b/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java @@ -0,0 +1,20 @@ +package g0901_1000.s0922_sort_array_by_parity_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sortArrayByParityII() { + assertThat( + new Solution().sortArrayByParityII(new int[] {4, 2, 5, 7}), + equalTo(new int[] {4, 5, 2, 7})); + } + + @Test + void sortArrayByParityII2() { + assertThat(new Solution().sortArrayByParityII(new int[] {2, 3}), equalTo(new int[] {2, 3})); + } +} diff --git a/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java b/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java new file mode 100644 index 000000000..f160119d3 --- /dev/null +++ b/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java @@ -0,0 +1,20 @@ +package g0901_1000.s0923_3sum_with_multiplicity; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void threeSumMulti() { + assertThat( + new Solution().threeSumMulti(new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8), + equalTo(20)); + } + + @Test + void threeSumMulti2() { + assertThat(new Solution().threeSumMulti(new int[] {1, 1, 2, 2, 2, 2}, 5), equalTo(12)); + } +} diff --git a/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java b/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java new file mode 100644 index 000000000..f63c6d895 --- /dev/null +++ b/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java @@ -0,0 +1,35 @@ +package g0901_1000.s0924_minimize_malware_spread; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMalwareSpread() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}, new int[] {0, 1}), + equalTo(0)); + } + + @Test + void minMalwareSpread2() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, new int[] {0, 2}), + equalTo(0)); + } + + @Test + void minMalwareSpread3() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, new int[] {1, 2}), + equalTo(1)); + } +} diff --git a/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java b/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java new file mode 100644 index 000000000..438efb04c --- /dev/null +++ b/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java @@ -0,0 +1,18 @@ +package g0901_1000.s0925_long_pressed_name; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isLongPressedName() { + assertThat(new Solution().isLongPressedName("alex", "aaleex"), equalTo(true)); + } + + @Test + void isLongPressedName2() { + assertThat(new Solution().isLongPressedName("saeed", "ssaaedd"), equalTo(false)); + } +} diff --git a/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java b/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java new file mode 100644 index 000000000..14cdde6fb --- /dev/null +++ b/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java @@ -0,0 +1,23 @@ +package g0901_1000.s0926_flip_string_to_monotone_increasing; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minFlipsMonoIncr() { + assertThat(new Solution().minFlipsMonoIncr("00110"), equalTo(1)); + } + + @Test + void minFlipsMonoIncr2() { + assertThat(new Solution().minFlipsMonoIncr("010110"), equalTo(2)); + } + + @Test + void minFlipsMonoIncr3() { + assertThat(new Solution().minFlipsMonoIncr("00011000"), equalTo(2)); + } +} From a878cdbfc6b2201b7ef9bf30aed992aa59bf8b13 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年2月14日 22:24:22 +0700 Subject: [PATCH 2/3] fix smells --- .../s0923_3sum_with_multiplicity/Solution.java | 12 ++++++------ .../Solution.java | 16 ++++++---------- .../Solution.java | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java index 8c340a78d..6cd942d00 100644 --- a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java @@ -6,25 +6,25 @@ public class Solution { public int threeSumMulti(int[] arr, int target) { int answer = 0; - int[] count_right = new int[MAX + 1]; + int[] countRight = new int[MAX + 1]; for (int num : arr) { - ++count_right[num]; + ++countRight[num]; } - int[] count_left = new int[MAX + 1]; + int[] countLeft = new int[MAX + 1]; for (int j = 0; j < arr.length - 1; j++) { - --count_right[arr[j]]; + --countRight[arr[j]]; int remains = target - arr[j]; if (remains <= 2 * MAX) { for (int v = 0; v <= Math.min(remains, MAX); v++) { if (remains - v <= MAX) { - int count = count_right[v] * count_left[remains - v]; + int count = countRight[v] * countLeft[remains - v]; if (count> 0) { answer = (answer + count) % MOD; } } } } - ++count_left[arr[j]]; + ++countLeft[arr[j]]; } return answer; } diff --git a/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java index 1cd437b1f..82bdbf711 100644 --- a/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java +++ b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java @@ -44,19 +44,15 @@ public int minMalwareSpread(int[][] graph, int[] initial) { if (par[lv][1]> max) { max = par[lv][1]; ans = val; - } else if (par[lv][1] == max) { - if (val < ans) { - ans = val; - } + } else if (par[lv][1] == max && val < ans) { + ans = val; } } else { if (max < 0) { max = 0; ans = val; - } else if (max == 0) { - if (val < ans) { - ans = val; - } + } else if (max == 0 && val < ans) { + ans = val; } } } @@ -68,7 +64,7 @@ private int find(int x) { if (par[x][0] == x) { return x; } - int temp = find(par[x][0]); - return par[x][0] = temp; + + return find(par[x][0]); } } diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java index 882848fa9..b5d6282a7 100644 --- a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java @@ -2,13 +2,19 @@ class Solution { public int minFlipsMonoIncr(String s) { - if (s == null || s.length() <= 1) return 0; - final int N = s.length(); - int countOnes = 0, countFlips = 0; - for (int i = 0; i < N; i++) { + if (s == null || s.length() <= 1) { + return 0; + } + final int n = s.length(); + int countOnes = 0; + int countFlips = 0; + for (int i = 0; i < n; i++) { if (s.charAt(i) == '0') { - if (countOnes == 0) continue; - else countFlips++; + if (countOnes == 0) { + continue; + } else { + countFlips++; + } } else { countOnes++; } From a3aca561e5f86524f9cf4d16c1465d3ea371e60f Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: 2022年2月14日 23:03:47 +0700 Subject: [PATCH 3/3] fix comments --- .../g0901_1000/s0922_sort_array_by_parity_ii/Solution.java | 4 +++- .../g0901_1000/s0923_3sum_with_multiplicity/Solution.java | 2 ++ .../s0926_flip_string_to_monotone_increasing/Solution.java | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java index 5786b6f9d..a71b20c13 100644 --- a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java +++ b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java @@ -4,7 +4,9 @@ public class Solution { public int[] sortArrayByParityII(int[] nums) { - for (int i = 0, j = 1; i < nums.length - 1 && j < nums.length; ) { + int i = 0; + int j = 1; + while (i < nums.length - 1 && j < nums.length) { if (nums[i] % 2 != 0 && nums[j] % 2 == 0) { int tmp = nums[i]; nums[i] = nums[j]; diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java index 6cd942d00..cddc62cd1 100644 --- a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java @@ -1,5 +1,7 @@ package g0901_1000.s0923_3sum_with_multiplicity; +// #Medium #Array #Hash_Table #Sorting #Two_Pointers #Counting + public class Solution { private static final int MOD = (int) 1e9 + 7; private static final int MAX = 100; diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java index b5d6282a7..00e58e82d 100644 --- a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java @@ -1,5 +1,7 @@ package g0901_1000.s0926_flip_string_to_monotone_increasing; +// #Medium #String #Dynamic_Programming + class Solution { public int minFlipsMonoIncr(String s) { if (s == null || s.length() <= 1) {

AltStyle によって変換されたページ (->オリジナル) /