diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md index d0f756c1a57a9..008460230c35f 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md @@ -80,7 +80,15 @@ tags: -### 方法一 +### 方法一:计数 + 枚举 + +我们先统计 $\textit{digits}$ 中每个数字出现的次数,记录在数组或哈希表 $\textit{cnt}$ 中。 + +然后,我们在 $[100, 1000)$ 的范围内枚举所有的偶数,判断这个偶数的每一位数字是否都不超过 $\textit{cnt}$ 中对应的数字的次数。如果是,则将这个偶数加入答案数组中。 + +最后,返回答案数组。 + +时间复杂度 $O(k \times 10^k)$,其中 $k$ 是目标偶数的位数,本题中 $k = 3$。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -89,17 +97,16 @@ tags: ```python class Solution: def findEvenNumbers(self, digits: List[int]) -> List[int]: + cnt = Counter(digits) ans = [] - counter = Counter(digits) - for i in range(100, 1000, 2): - t = [] - k = i - while k: - t.append(k % 10) - k //= 10 - cnt = Counter(t) - if all([counter[i] >= cnt[i] for i in range(10)]): - ans.append(i) + for x in range(100, 1000, 2): + cnt1 = Counter() + y = x + while y: + y, v = divmod(y, 10) + cnt1[v] += 1 + if all(cnt[i] >= cnt1[i] for i in range(10)): + ans.append(x) return ans ``` @@ -108,37 +115,25 @@ class Solution: ```java class Solution { public int[] findEvenNumbers(int[] digits) { - int[] counter = count(digits); + int[] cnt = new int[10]; + for (int x : digits) { + ++cnt[x]; + } List ans = new ArrayList<>(); - for (int i = 100; i < 1000; i += 2) { - int[] t = new int[3]; - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int[] cnt1 = new int[10]; + for (int y = x; y > 0; y /= 10) { + ++cnt1[y % 10]; } - int[] cnt = count(t); - if (check(counter, cnt)) { - ans.add(i); + boolean ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; } - } - return ans.stream().mapToInt(Integer::valueOf).toArray(); - } - - private boolean check(int[] cnt1, int[] cnt2) { - for (int i = 0; i < 10; ++i) { - if (cnt1[i] < cnt2[i]) { - return false; + if (ok) { + ans.add(x); } } - return true; - } - - private int[] count(int[] nums) { - int[] counter = new int[10]; - for (int num : nums) { - ++counter[num]; - } - return counter; + return ans.stream().mapToInt(i -> i).toArray(); } } ``` @@ -149,71 +144,51 @@ class Solution { class Solution { public: vector findEvenNumbers(vector& digits) { - vector counter = count(digits); + int cnt[10]{}; + for (int x : digits) { + ++cnt[x]; + } vector ans; - for (int i = 100; i < 1000; i += 2) { - vector t(3); - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int cnt1[10]{}; + for (int y = x; y; y /= 10) { + ++cnt1[y % 10]; + } + bool ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push_back(x); } - vector cnt = count(t); - if (check(counter, cnt)) ans.push_back(i); } return ans; } - - vector count(vector& nums) { - vector counter(10); - for (int num : nums) ++counter[num]; - return counter; - } - - bool check(vector& cnt1, vector& cnt2) { - for (int i = 0; i < 10; ++i) - if (cnt1[i] < cnt2[i]) - return false; - return true; - } }; ``` #### Go ```go -func findEvenNumbers(digits []int) []int { - counter := count(digits) - var ans []int - for i := 100; i < 1000; i += 2 { - t := make([]int, 3) - k := i - for j := 0; k > 0; j++ { - t[j] = k % 10 - k /= 10 +func findEvenNumbers(digits []int) (ans []int) { + cnt := [10]int{} + for _, x := range digits { + cnt[x]++ + } + for x := 100; x < 1000; x += 2 { + cnt1 := [10]int{} + for y := x; y > 0; y /= 10 { + cnt1[y%10]++ } - cnt := count(t) - if check(counter, cnt) { - ans = append(ans, i) + ok := true + for i := 0; i < 10 && ok; i++ { + ok = cnt[i] >= cnt1[i] } - } - return ans -} - -func count(nums []int) []int { - counter := make([]int, 10) - for _, num := range nums { - counter[num]++ - } - return counter -} - -func check(cnt1, cnt2 []int) bool { - for i := 0; i < 10; i++ { - if cnt1[i] < cnt2[i] { - return false + if ok { + ans = append(ans, x) } } - return true + return } ``` @@ -221,30 +196,56 @@ func check(cnt1, cnt2 []int) bool { ```ts function findEvenNumbers(digits: number[]): number[] { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; + const cnt: number[] = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; } - let ans = []; - for (let i = 100; i < 1000; i += 2) { - if (check(record, String(i))) { - ans.push(i); + const ans: number[] = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1: number[] = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); } } return ans; } +``` -function check(target: Array, digits: string): boolean { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; +#### JavaScript + +```js +/** + * @param {number[]} digits + * @return {number[]} + */ +var findEvenNumbers = function (digits) { + const cnt = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; } - - for (let i = 0; i < 10; i++) { - if (record[i] > target[i]) return false; + const ans = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1 = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); + } } - return true; -} + return ans; +}; ``` diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README_EN.md b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README_EN.md index ee8e5f560b6f3..fb7079434fd7e 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README_EN.md +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/README_EN.md @@ -76,7 +76,15 @@ In this example, the digit 8 is used twice each time in 288, 828, and 882. -### Solution 1 +### Solution 1: Counting + Enumeration + +First, we count the occurrence of each digit in $\textit{digits}$, recording it in an array or hash table $\textit{cnt}$. + +Then, we enumerate all even numbers in the range $[100, 1000)$, checking if each digit of the even number does not exceed the corresponding digit's count in $\textit{cnt}$. If so, we add this even number to the answer array. + +Finally, we return the answer array. + +The time complexity is $O(k \times 10^k)$, where $k$ is the number of digits of the target even number, which is $3$ in this problem. Ignoring the space consumed by the answer, the space complexity is $O(1)$. @@ -85,17 +93,16 @@ In this example, the digit 8 is used twice each time in 288, 828, and 882. ```python class Solution: def findEvenNumbers(self, digits: List[int]) -> List[int]: + cnt = Counter(digits) ans = [] - counter = Counter(digits) - for i in range(100, 1000, 2): - t = [] - k = i - while k: - t.append(k % 10) - k //= 10 - cnt = Counter(t) - if all([counter[i] >= cnt[i] for i in range(10)]): - ans.append(i) + for x in range(100, 1000, 2): + cnt1 = Counter() + y = x + while y: + y, v = divmod(y, 10) + cnt1[v] += 1 + if all(cnt[i] >= cnt1[i] for i in range(10)): + ans.append(x) return ans ``` @@ -104,37 +111,25 @@ class Solution: ```java class Solution { public int[] findEvenNumbers(int[] digits) { - int[] counter = count(digits); + int[] cnt = new int[10]; + for (int x : digits) { + ++cnt[x]; + } List ans = new ArrayList<>(); - for (int i = 100; i < 1000; i += 2) { - int[] t = new int[3]; - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int[] cnt1 = new int[10]; + for (int y = x; y > 0; y /= 10) { + ++cnt1[y % 10]; } - int[] cnt = count(t); - if (check(counter, cnt)) { - ans.add(i); + boolean ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; } - } - return ans.stream().mapToInt(Integer::valueOf).toArray(); - } - - private boolean check(int[] cnt1, int[] cnt2) { - for (int i = 0; i < 10; ++i) { - if (cnt1[i] < cnt2[i]) { - return false; + if (ok) { + ans.add(x); } } - return true; - } - - private int[] count(int[] nums) { - int[] counter = new int[10]; - for (int num : nums) { - ++counter[num]; - } - return counter; + return ans.stream().mapToInt(i -> i).toArray(); } } ``` @@ -145,71 +140,51 @@ class Solution { class Solution { public: vector findEvenNumbers(vector& digits) { - vector counter = count(digits); + int cnt[10]{}; + for (int x : digits) { + ++cnt[x]; + } vector ans; - for (int i = 100; i < 1000; i += 2) { - vector t(3); - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int cnt1[10]{}; + for (int y = x; y; y /= 10) { + ++cnt1[y % 10]; + } + bool ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push_back(x); } - vector cnt = count(t); - if (check(counter, cnt)) ans.push_back(i); } return ans; } - - vector count(vector& nums) { - vector counter(10); - for (int num : nums) ++counter[num]; - return counter; - } - - bool check(vector& cnt1, vector& cnt2) { - for (int i = 0; i < 10; ++i) - if (cnt1[i] < cnt2[i]) - return false; - return true; - } }; ``` #### Go ```go -func findEvenNumbers(digits []int) []int { - counter := count(digits) - var ans []int - for i := 100; i < 1000; i += 2 { - t := make([]int, 3) - k := i - for j := 0; k > 0; j++ { - t[j] = k % 10 - k /= 10 +func findEvenNumbers(digits []int) (ans []int) { + cnt := [10]int{} + for _, x := range digits { + cnt[x]++ + } + for x := 100; x < 1000; x += 2 { + cnt1 := [10]int{} + for y := x; y > 0; y /= 10 { + cnt1[y%10]++ } - cnt := count(t) - if check(counter, cnt) { - ans = append(ans, i) + ok := true + for i := 0; i < 10 && ok; i++ { + ok = cnt[i] >= cnt1[i] } - } - return ans -} - -func count(nums []int) []int { - counter := make([]int, 10) - for _, num := range nums { - counter[num]++ - } - return counter -} - -func check(cnt1, cnt2 []int) bool { - for i := 0; i < 10; i++ { - if cnt1[i] < cnt2[i] { - return false + if ok { + ans = append(ans, x) } } - return true + return } ``` @@ -217,30 +192,56 @@ func check(cnt1, cnt2 []int) bool { ```ts function findEvenNumbers(digits: number[]): number[] { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; + const cnt: number[] = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; } - let ans = []; - for (let i = 100; i < 1000; i += 2) { - if (check(record, String(i))) { - ans.push(i); + const ans: number[] = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1: number[] = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); } } return ans; } +``` -function check(target: Array, digits: string): boolean { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; +#### JavaScript + +```js +/** + * @param {number[]} digits + * @return {number[]} + */ +var findEvenNumbers = function (digits) { + const cnt = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; } - - for (let i = 0; i < 10; i++) { - if (record[i] > target[i]) return false; + const ans = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1 = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); + } } - return true; -} + return ans; +}; ``` diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp index eb62909788538..8dc6cd3cd9959 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp @@ -1,30 +1,24 @@ class Solution { public: vector findEvenNumbers(vector& digits) { - vector counter = count(digits); + int cnt[10]{}; + for (int x : digits) { + ++cnt[x]; + } vector ans; - for (int i = 100; i < 1000; i += 2) { - vector t(3); - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int cnt1[10]{}; + for (int y = x; y; y /= 10) { + ++cnt1[y % 10]; + } + bool ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push_back(x); } - vector cnt = count(t); - if (check(counter, cnt)) ans.push_back(i); } return ans; } - - vector count(vector& nums) { - vector counter(10); - for (int num : nums) ++counter[num]; - return counter; - } - - bool check(vector& cnt1, vector& cnt2) { - for (int i = 0; i < 10; ++i) - if (cnt1[i] < cnt2[i]) - return false; - return true; - } }; \ No newline at end of file diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.go b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.go index 5ea9c776dfab9..81d3e42fa1660 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.go +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.go @@ -1,34 +1,20 @@ -func findEvenNumbers(digits []int) []int { - counter := count(digits) - var ans []int - for i := 100; i < 1000; i += 2 { - t := make([]int, 3) - k := i - for j := 0; k > 0; j++ { - t[j] = k % 10 - k /= 10 +func findEvenNumbers(digits []int) (ans []int) { + cnt := [10]int{} + for _, x := range digits { + cnt[x]++ + } + for x := 100; x < 1000; x += 2 { + cnt1 := [10]int{} + for y := x; y > 0; y /= 10 { + cnt1[y%10]++ } - cnt := count(t) - if check(counter, cnt) { - ans = append(ans, i) + ok := true + for i := 0; i < 10 && ok; i++ { + ok = cnt[i] >= cnt1[i] } - } - return ans -} - -func count(nums []int) []int { - counter := make([]int, 10) - for _, num := range nums { - counter[num]++ - } - return counter -} - -func check(cnt1, cnt2 []int) bool { - for i := 0; i < 10; i++ { - if cnt1[i] < cnt2[i] { - return false + if ok { + ans = append(ans, x) } } - return true + return } \ No newline at end of file diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.java b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.java index 8aa69394d9342..faf22b41d2df3 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.java +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.java @@ -1,35 +1,23 @@ class Solution { public int[] findEvenNumbers(int[] digits) { - int[] counter = count(digits); + int[] cnt = new int[10]; + for (int x : digits) { + ++cnt[x]; + } List ans = new ArrayList<>(); - for (int i = 100; i < 1000; i += 2) { - int[] t = new int[3]; - for (int j = 0, k = i; k > 0; ++j) { - t[j] = k % 10; - k /= 10; + for (int x = 100; x < 1000; x += 2) { + int[] cnt1 = new int[10]; + for (int y = x; y > 0; y /= 10) { + ++cnt1[y % 10]; } - int[] cnt = count(t); - if (check(counter, cnt)) { - ans.add(i); + boolean ok = true; + for (int i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; } - } - return ans.stream().mapToInt(Integer::valueOf).toArray(); - } - - private boolean check(int[] cnt1, int[] cnt2) { - for (int i = 0; i < 10; ++i) { - if (cnt1[i] < cnt2[i]) { - return false; + if (ok) { + ans.add(x); } } - return true; - } - - private int[] count(int[] nums) { - int[] counter = new int[10]; - for (int num : nums) { - ++counter[num]; - } - return counter; + return ans.stream().mapToInt(i -> i).toArray(); } } \ No newline at end of file diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.js b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.js new file mode 100644 index 0000000000000..6643c301be76a --- /dev/null +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} digits + * @return {number[]} + */ +var findEvenNumbers = function (digits) { + const cnt = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; + } + const ans = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1 = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); + } + } + return ans; +}; diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.py b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.py index 1060bac2963d7..27d01eb4a03f8 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.py +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.py @@ -1,14 +1,13 @@ class Solution: def findEvenNumbers(self, digits: List[int]) -> List[int]: + cnt = Counter(digits) ans = [] - counter = Counter(digits) - for i in range(100, 1000, 2): - t = [] - k = i - while k: - t.append(k % 10) - k //= 10 - cnt = Counter(t) - if all([counter[i] >= cnt[i] for i in range(10)]): - ans.append(i) + for x in range(100, 1000, 2): + cnt1 = Counter() + y = x + while y: + y, v = divmod(y, 10) + cnt1[v] += 1 + if all(cnt[i] >= cnt1[i] for i in range(10)): + ans.append(x) return ans diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.ts b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.ts index c90ca3ad34164..54a1e2bd0576e 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.ts +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.ts @@ -1,25 +1,21 @@ function findEvenNumbers(digits: number[]): number[] { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; + const cnt: number[] = Array(10).fill(0); + for (const x of digits) { + ++cnt[x]; } - let ans = []; - for (let i = 100; i < 1000; i += 2) { - if (check(record, String(i))) { - ans.push(i); + const ans: number[] = []; + for (let x = 100; x < 1000; x += 2) { + const cnt1: number[] = Array(10).fill(0); + for (let y = x; y; y = Math.floor(y / 10)) { + ++cnt1[y % 10]; + } + let ok = true; + for (let i = 0; i < 10 && ok; ++i) { + ok = cnt[i] >= cnt1[i]; + } + if (ok) { + ans.push(x); } } return ans; } - -function check(target: Array, digits: string): boolean { - let record = new Array(10).fill(0); - for (let digit of digits) { - record[digit]++; - } - - for (let i = 0; i < 10; i++) { - if (record[i] > target[i]) return false; - } - return true; -} diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md index 4121be84afe0c..3c166df9b2d46 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md @@ -94,7 +94,7 @@ tags: 接下来,我们遍历所有炸弹,对于每个炸弹 $k$,我们使用广度优先搜索计算炸弹 $k$ 的爆炸范围内可以引爆的所有炸弹的下标,并记录下来。如果这些炸弹的数量等于 $n$,那么我们就可以引爆所有炸弹,直接返回 $n$。否则,我们记录下来这些炸弹的数量,并返回最大值。 -时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为炸弹的数量。 +时间复杂度 $O(n^3)$,空间复杂度 $O(n^2)$。其中 $n$ 为炸弹的数量。 diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md index 56cd25ab4440e..178ad7fa58a09 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md @@ -89,7 +89,7 @@ Next, we iterate over all bombs. For two bombs $(x_1, y_1, r_1)$ and $(x_2, y_2, Next, we iterate over all bombs. For each bomb $k$, we use breadth-first search to calculate the indices of all bombs that can be triggered by bomb $k$ within its explosion range and record them. If the number of these bombs equals $n$, then we can trigger all bombs and directly return $n$. Otherwise, we record the number of these bombs and return the maximum value. -The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the number of bombs. +The time complexity is $O(n^3)$ and the space complexity is $O(n^2)$, where $n$ is the number of bombs.