Skip to content

feat: add solutions to lc problem: No.2094 #3304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 104 additions & 103 deletions solution/2000-2099/2094.Finding 3-Digit Even Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:计数 + 枚举

我们先统计 $\textit{digits}$ 中每个数字出现的次数,记录在数组或哈希表 $\textit{cnt}$ 中。

然后,我们在 $[100, 1000)$ 的范围内枚举所有的偶数,判断这个偶数的每一位数字是否都不超过 $\textit{cnt}$ 中对应的数字的次数。如果是,则将这个偶数加入答案数组中。

最后,返回答案数组。

时间复杂度 $O(k \times 10^k)$,其中 $k$ 是目标偶数的位数,本题中 $k = 3$。忽略答案的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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<Integer> 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();
}
}
```
Expand All @@ -149,102 +144,108 @@ class Solution {
class Solution {
public:
vector<int> findEvenNumbers(vector<int>& digits) {
vector<int> counter = count(digits);
int cnt[10]{};
for (int x : digits) {
++cnt[x];
}
vector<int> ans;
for (int i = 100; i < 1000; i += 2) {
vector<int> 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<int> cnt = count(t);
if (check(counter, cnt)) ans.push_back(i);
}
return ans;
}

vector<int> count(vector<int>& nums) {
vector<int> counter(10);
for (int num : nums) ++counter[num];
return counter;
}

bool check(vector<int>& cnt1, vector<int>& 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
}
```

#### TypeScript

```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<number>, 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;
};
```

<!-- tabs:end -->
Expand Down
Loading
Loading