Skip to content

Commit 84ae9d4

Browse files
committed
feat: add solutions to lc problems: No.2094~2096
* No.2094.Finding 3-Digit Even Numbers * No.2095.Delete the Middle Node of a Linked List * No.2096.Step-By-Step Directions From a Binary Tree Node to Another
1 parent 6febd63 commit 84ae9d4

File tree

23 files changed

+1351
-29
lines changed

23 files changed

+1351
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# [2094. 找出 3 位偶数](https://leetcode-cn.com/problems/finding-3-digit-even-numbers)
2+
3+
[English Version](/solution/2000-2099/2094.Finding%203-Digit%20Even%20Numbers/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组 <code>digits</code> ,其中每个元素是一个数字(<code>0 - 9</code>)。数组中可能存在重复元素。</p>
10+
11+
<p>你需要找出 <strong>所有</strong> 满足下述条件且 <strong>互不相同</strong> 的整数:</p>
12+
13+
<ul>
14+
<li>该整数由 <code>digits</code> 中的三个元素按 <strong>任意</strong> 顺序 <strong>依次连接</strong> 组成。</li>
15+
<li>该整数不含 <strong>前导零</strong></li>
16+
<li>该整数是一个 <strong>偶数</strong></li>
17+
</ul>
18+
19+
<p>例如,给定的 <code>digits</code> 是 <code>[1, 2, 3]</code> ,整数 <code>132</code> 和 <code>312</code> 满足上面列出的全部条件。</p>
20+
21+
<p>将找出的所有互不相同的整数按 <strong>递增顺序</strong> 排列,并以数组形式返回<em>。</em></p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>digits = [2,1,3,0]
29+
<strong>输出:</strong>[102,120,130,132,210,230,302,310,312,320]
30+
<strong>解释:</strong>
31+
所有满足题目条件的整数都在输出数组中列出。
32+
注意,答案数组中不含有 <strong>奇数</strong> 或带 <strong>前导零</strong> 的整数。</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<pre>
37+
<strong>输入:</strong>digits = [2,2,8,8,2]
38+
<strong>输出:</strong>[222,228,282,288,822,828,882]
39+
<strong>解释:</strong>
40+
同样的数字(0 - 9)在构造整数时可以重复多次,重复次数最多与其在 <code>digits</code> 中出现的次数一样。
41+
在这个例子中,数字 8 在构造 288、828 和 882 时都重复了两次。
42+
</pre>
43+
44+
<p><strong>示例 3:</strong></p>
45+
46+
<pre>
47+
<strong>输入:</strong>digits = [3,7,5]
48+
<strong>输出:</strong>[]
49+
<strong>解释:</strong>
50+
使用给定的 digits 无法构造偶数。
51+
</pre>
52+
53+
<p><strong>示例 4:</strong></p>
54+
55+
<pre>
56+
<strong>输入:</strong>digits = [0,2,0,0]
57+
<strong>输出:</strong>[200]
58+
<strong>解释:</strong>
59+
唯一一个不含 <strong>前导零</strong> 且满足全部条件的整数是 200 。
60+
</pre>
61+
62+
<p><strong>示例 5:</strong></p>
63+
64+
<pre>
65+
<strong>输入:</strong>digits = [0,0,0]
66+
<strong>输出:</strong>[]
67+
<strong>解释:</strong>
68+
构造的所有整数都会有 <strong>前导零</strong> 。因此,不存在满足题目条件的整数。
69+
</pre>
70+
71+
<p>&nbsp;</p>
72+
73+
<p><strong>提示:</strong></p>
74+
75+
<ul>
76+
<li><code>3 &lt;=&nbsp;digits.length &lt;= 100</code></li>
77+
<li><code>0 &lt;= digits[i] &lt;= 9</code></li>
78+
</ul>
79+
80+
## 解法
81+
82+
<!-- 这里可写通用的实现逻辑 -->
83+
84+
<!-- tabs:start -->
85+
86+
### **Python3**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```python
91+
class Solution:
92+
def findEvenNumbers(self, digits: List[int]) -> List[int]:
93+
ans = []
94+
counter = Counter(digits)
95+
for i in range(100, 1000, 2):
96+
t = []
97+
k = i
98+
while k:
99+
t.append(k % 10)
100+
k //= 10
101+
cnt = Counter(t)
102+
if all([counter[i] >= cnt[i] for i in range(10)]):
103+
ans.append(i)
104+
return ans
105+
```
106+
107+
### **Java**
108+
109+
<!-- 这里可写当前语言的特殊实现逻辑 -->
110+
111+
```java
112+
class Solution {
113+
public int[] findEvenNumbers(int[] digits) {
114+
int[] counter = count(digits);
115+
List<Integer> ans = new ArrayList<>();
116+
for (int i = 100; i < 1000; i += 2) {
117+
int[] t = new int[3];
118+
for (int j = 0, k = i; k > 0; ++j) {
119+
t[j] = k % 10;
120+
k /= 10;
121+
}
122+
int[] cnt = count(t);
123+
if (check(counter, cnt)) {
124+
ans.add(i);
125+
}
126+
}
127+
return ans.stream().mapToInt(Integer::valueOf).toArray();
128+
}
129+
130+
private boolean check(int[] cnt1, int[] cnt2) {
131+
for (int i = 0; i < 10; ++i) {
132+
if (cnt1[i] < cnt2[i]) {
133+
return false;
134+
}
135+
}
136+
return true;
137+
}
138+
139+
private int[] count(int[] nums) {
140+
int[] counter = new int[10];
141+
for (int num : nums) {
142+
++counter[num];
143+
}
144+
return counter;
145+
}
146+
}
147+
```
148+
149+
### **TypeScript**
150+
151+
<!-- 这里可写当前语言的特殊实现逻辑 -->
152+
153+
```ts
154+
155+
```
156+
157+
### **...**
158+
159+
```
160+
161+
```
162+
163+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# [2094. Finding 3-Digit Even Numbers](https://leetcode.com/problems/finding-3-digit-even-numbers)
2+
3+
[中文文档](/solution/2000-2099/2094.Finding%203-Digit%20Even%20Numbers/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer array <code>digits</code>, where each element is a digit. The array may contain duplicates.</p>
8+
9+
<p>You need to find <strong>all</strong> the <strong>unique</strong> integers that follow the given requirements:</p>
10+
11+
<ul>
12+
<li>The integer consists of the <strong>concatenation</strong> of <strong>three</strong> elements from <code>digits</code> in <strong>any</strong> arbitrary order.</li>
13+
<li>The integer does not have <strong>leading zeros</strong>.</li>
14+
<li>The integer is <strong>even</strong>.</li>
15+
</ul>
16+
17+
<p>For example, if the given <code>digits</code> were <code>[1, 2, 3]</code>, integers <code>132</code> and <code>312</code> follow the requirements.</p>
18+
19+
<p>Return <em>a <strong>sorted</strong> array of the unique integers.</em></p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong>Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> digits = [2,1,3,0]
26+
<strong>Output:</strong> [102,120,130,132,210,230,302,310,312,320]
27+
<strong>Explanation:</strong>
28+
All the possible integers that follow the requirements are in the output array.
29+
Notice that there are no <strong>odd</strong> integers or integers with <strong>leading zeros</strong>.</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> digits = [2,2,8,8,2]
35+
<strong>Output:</strong> [222,228,282,288,822,828,882]
36+
<strong>Explanation:</strong>
37+
The same digit can be used as many times as it appears in <code>digits</code>.
38+
In this example, the digit 8 is used twice each time in 288, 828, and 882.
39+
</pre>
40+
41+
<p><strong>Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> digits = [3,7,5]
45+
<strong>Output:</strong> []
46+
<strong>Explanation:</strong>
47+
No <strong>even</strong> integers can be formed using the given digits.
48+
</pre>
49+
50+
<p><strong>Example 4:</strong></p>
51+
52+
<pre>
53+
<strong>Input:</strong> digits = [0,2,0,0]
54+
<strong>Output:</strong> [200]
55+
<strong>Explanation:</strong>
56+
The only valid integer that can be formed with three digits and <strong>no leading zeros</strong> is 200.
57+
</pre>
58+
59+
<p><strong>Example 5:</strong></p>
60+
61+
<pre>
62+
<strong>Input:</strong> digits = [0,0,0]
63+
<strong>Output:</strong> []
64+
<strong>Explanation:</strong>
65+
All the integers that can be formed have <strong>leading zeros</strong>. Thus, there are no valid integers.
66+
</pre>
67+
68+
<p>&nbsp;</p>
69+
<p><strong>Constraints:</strong></p>
70+
71+
<ul>
72+
<li><code>3 &lt;=&nbsp;digits.length &lt;= 100</code></li>
73+
<li><code>0 &lt;= digits[i] &lt;= 9</code></li>
74+
</ul>
75+
76+
## Solutions
77+
78+
<!-- tabs:start -->
79+
80+
### **Python3**
81+
82+
```python
83+
class Solution:
84+
def findEvenNumbers(self, digits: List[int]) -> List[int]:
85+
ans = []
86+
counter = Counter(digits)
87+
for i in range(100, 1000, 2):
88+
t = []
89+
k = i
90+
while k:
91+
t.append(k % 10)
92+
k //= 10
93+
cnt = Counter(t)
94+
if all([counter[i] >= cnt[i] for i in range(10)]):
95+
ans.append(i)
96+
return ans
97+
```
98+
99+
### **Java**
100+
101+
```java
102+
class Solution {
103+
public int[] findEvenNumbers(int[] digits) {
104+
int[] counter = count(digits);
105+
List<Integer> ans = new ArrayList<>();
106+
for (int i = 100; i < 1000; i += 2) {
107+
int[] t = new int[3];
108+
for (int j = 0, k = i; k > 0; ++j) {
109+
t[j] = k % 10;
110+
k /= 10;
111+
}
112+
int[] cnt = count(t);
113+
if (check(counter, cnt)) {
114+
ans.add(i);
115+
}
116+
}
117+
return ans.stream().mapToInt(Integer::valueOf).toArray();
118+
}
119+
120+
private boolean check(int[] cnt1, int[] cnt2) {
121+
for (int i = 0; i < 10; ++i) {
122+
if (cnt1[i] < cnt2[i]) {
123+
return false;
124+
}
125+
}
126+
return true;
127+
}
128+
129+
private int[] count(int[] nums) {
130+
int[] counter = new int[10];
131+
for (int num : nums) {
132+
++counter[num];
133+
}
134+
return counter;
135+
}
136+
}
137+
```
138+
139+
### **TypeScript**
140+
141+
```ts
142+
143+
```
144+
145+
### **...**
146+
147+
```
148+
149+
```
150+
151+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int[] findEvenNumbers(int[] digits) {
3+
int[] counter = count(digits);
4+
List<Integer> ans = new ArrayList<>();
5+
for (int i = 100; i < 1000; i += 2) {
6+
int[] t = new int[3];
7+
for (int j = 0, k = i; k > 0; ++j) {
8+
t[j] = k % 10;
9+
k /= 10;
10+
}
11+
int[] cnt = count(t);
12+
if (check(counter, cnt)) {
13+
ans.add(i);
14+
}
15+
}
16+
return ans.stream().mapToInt(Integer::valueOf).toArray();
17+
}
18+
19+
private boolean check(int[] cnt1, int[] cnt2) {
20+
for (int i = 0; i < 10; ++i) {
21+
if (cnt1[i] < cnt2[i]) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
private int[] count(int[] nums) {
29+
int[] counter = new int[10];
30+
for (int num : nums) {
31+
++counter[num];
32+
}
33+
return counter;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findEvenNumbers(self, digits: List[int]) -> List[int]:
3+
ans = []
4+
counter = Counter(digits)
5+
for i in range(100, 1000, 2):
6+
t = []
7+
k = i
8+
while k:
9+
t.append(k % 10)
10+
k //= 10
11+
cnt = Counter(t)
12+
if all([counter[i] >= cnt[i] for i in range(10)]):
13+
ans.append(i)
14+
return ans

0 commit comments

Comments
 (0)