8080
8181<!-- solution:start -->
8282
83- ### 方法一
83+ ### 方法一:计数 + 枚举
84+
85+ 我们先统计 $\textit{digits}$ 中每个数字出现的次数,记录在数组或哈希表 $\textit{cnt}$ 中。
86+
87+ 然后,我们在 $[ 100, 1000)$ 的范围内枚举所有的偶数,判断这个偶数的每一位数字是否都不超过 $\textit{cnt}$ 中对应的数字的次数。如果是,则将这个偶数加入答案数组中。
88+
89+ 最后,返回答案数组。
90+
91+ 时间复杂度 $O(k \times 10^k)$,其中 $k$ 是目标偶数的位数,本题中 $k = 3$。忽略答案的空间消耗,空间复杂度 $O(1)$。
8492
8593<!-- tabs:start -->
8694
@@ -89,17 +97,16 @@ tags:
8997``` python
9098class Solution :
9199 def findEvenNumbers (self , digits : List[int ]) -> List[int ]:
100+ cnt = Counter(digits)
92101 ans = []
93- counter = Counter(digits)
94- for i in range (100 , 1000 , 2 ):
95- t = []
96- k = i
97- while k:
98- t.append(k % 10 )
99- k //= 10
100- cnt = Counter(t)
101- if all ([counter[i] >= cnt[i] for i in range (10 )]):
102- ans.append(i)
102+ for x in range (100 , 1000 , 2 ):
103+ cnt1 = Counter()
104+ y = x
105+ while y:
106+ y, v = divmod (y, 10 )
107+ cnt1[v] += 1
108+ if all (cnt[i] >= cnt1[i] for i in range (10 )):
109+ ans.append(x)
103110 return ans
104111```
105112
@@ -108,37 +115,25 @@ class Solution:
108115``` java
109116class Solution {
110117 public int [] findEvenNumbers (int [] digits ) {
111- int [] counter = count(digits);
118+ int [] cnt = new int [10 ];
119+ for (int x : digits) {
120+ ++ cnt[x];
121+ }
112122 List<Integer > ans = new ArrayList<> ();
113- for (int i = 100 ; i < 1000 ; i += 2 ) {
114- int [] t = new int [3 ];
115- for (int j = 0 , k = i; k > 0 ; ++ j) {
116- t[j] = k % 10 ;
117- k /= 10 ;
123+ for (int x = 100 ; x < 1000 ; x += 2 ) {
124+ int [] cnt1 = new int [10 ];
125+ for (int y = x; y > 0 ; y /= 10 ) {
126+ ++ cnt1[y % 10 ];
118127 }
119- int [] cnt = count(t) ;
120- if (check(counter, cnt) ) {
121- ans . add(i) ;
128+ boolean ok = true ;
129+ for ( int i = 0 ; i < 10 && ok; ++ i ) {
130+ ok = cnt[i] >= cnt1[i] ;
122131 }
123- }
124- return ans. stream(). mapToInt(Integer :: valueOf). toArray();
125- }
126-
127- private boolean check (int [] cnt1 , int [] cnt2 ) {
128- for (int i = 0 ; i < 10 ; ++ i) {
129- if (cnt1[i] < cnt2[i]) {
130- return false ;
132+ if (ok) {
133+ ans. add(x);
131134 }
132135 }
133- return true ;
134- }
135-
136- private int [] count (int [] nums ) {
137- int [] counter = new int [10 ];
138- for (int num : nums) {
139- ++ counter[num];
140- }
141- return counter;
136+ return ans. stream(). mapToInt(i - > i). toArray();
142137 }
143138}
144139```
@@ -149,102 +144,108 @@ class Solution {
149144class Solution {
150145public:
151146 vector<int > findEvenNumbers(vector<int >& digits) {
152- vector<int > counter = count(digits);
147+ int cnt[ 10] {};
148+ for (int x : digits) {
149+ ++cnt[ x] ;
150+ }
153151 vector<int > ans;
154- for (int i = 100; i < 1000; i += 2) {
155- vector<int > t(3);
156- for (int j = 0, k = i; k > 0; ++j) {
157- t[ j] = k % 10;
158- k /= 10;
152+ for (int x = 100; x < 1000; x += 2) {
153+ int cnt1[ 10] {};
154+ for (int y = x; y; y /= 10) {
155+ ++cnt1[ y % 10] ;
156+ }
157+ bool ok = true;
158+ for (int i = 0; i < 10 && ok; ++i) {
159+ ok = cnt[ i] >= cnt1[ i] ;
160+ }
161+ if (ok) {
162+ ans.push_back(x);
159163 }
160- vector<int > cnt = count(t);
161- if (check(counter, cnt)) ans.push_back(i);
162164 }
163165 return ans;
164166 }
165-
166- vector<int> count(vector<int>& nums) {
167- vector<int> counter(10);
168- for (int num : nums) ++counter[num];
169- return counter;
170- }
171-
172- bool check (vector<int >& cnt1, vector<int >& cnt2) {
173- for (int i = 0; i < 10; ++i)
174- if (cnt1[ i] < cnt2[ i] )
175- return false;
176- return true;
177- }
178167};
179168```
180169
181170#### Go
182171
183172```go
184- func findEvenNumbers(digits []int) []int {
185- counter := count(digits)
186- var ans []int
187- for i := 100; i < 1000; i += 2 {
188- t := make([]int, 3)
189- k := i
190- for j := 0; k > 0; j++ {
191- t[j] = k % 10
192- k /= 10
173+ func findEvenNumbers(digits []int) (ans []int) {
174+ cnt := [10]int{}
175+ for _, x := range digits {
176+ cnt[x]++
177+ }
178+ for x := 100; x < 1000; x += 2 {
179+ cnt1 := [10]int{}
180+ for y := x; y > 0; y /= 10 {
181+ cnt1[y%10]++
193182 }
194- cnt := count(t)
195- if check(counter, cnt) {
196- ans = append(ans, i)
183+ ok := true
184+ for i := 0; i < 10 && ok; i++ {
185+ ok = cnt[i] >= cnt1[i]
197186 }
198- }
199- return ans
200- }
201-
202- func count(nums []int) []int {
203- counter := make([]int, 10)
204- for _, num := range nums {
205- counter[num]++
206- }
207- return counter
208- }
209-
210- func check(cnt1, cnt2 []int) bool {
211- for i := 0; i < 10; i++ {
212- if cnt1[i] < cnt2[i] {
213- return false
187+ if ok {
188+ ans = append(ans, x)
214189 }
215190 }
216- return true
191+ return
217192}
218193```
219194
220195#### TypeScript
221196
222197``` ts
223198function findEvenNumbers(digits : number []): number [] {
224- let record = new Array (10 ).fill (0 );
225- for (let digit of digits ) {
226- record [ digit ] ++ ;
199+ const cnt : number [] = Array (10 ).fill (0 );
200+ for (const x of digits ) {
201+ ++ cnt [ x ] ;
227202 }
228- let ans = [];
229- for (let i = 100 ; i < 1000 ; i += 2 ) {
230- if (check (record , String (i ))) {
231- ans .push (i );
203+ const ans: number [] = [];
204+ for (let x = 100 ; x < 1000 ; x += 2 ) {
205+ const cnt1: number [] = Array (10 ).fill (0 );
206+ for (let y = x ; y ; y = Math .floor (y / 10 )) {
207+ ++ cnt1 [y % 10 ];
208+ }
209+ let ok = true ;
210+ for (let i = 0 ; i < 10 && ok ; ++ i ) {
211+ ok = cnt [i ] >= cnt1 [i ];
212+ }
213+ if (ok ) {
214+ ans .push (x );
232215 }
233216 }
234217 return ans ;
235218}
219+ ```
236220
237- function check(target : Array <number >, digits : string ): boolean {
238- let record = new Array (10 ).fill (0 );
239- for (let digit of digits ) {
240- record [digit ]++ ;
221+ #### JavaScript
222+
223+ ``` js
224+ /**
225+ * @param {number[]} digits
226+ * @return {number[]}
227+ */
228+ var findEvenNumbers = function (digits ) {
229+ const cnt = Array (10 ).fill (0 );
230+ for (const x of digits) {
231+ ++ cnt[x];
241232 }
242-
243- for (let i = 0 ; i < 10 ; i ++ ) {
244- if (record [i ] > target [i ]) return false ;
233+ const ans = [];
234+ for (let x = 100 ; x < 1000 ; x += 2 ) {
235+ const cnt1 = Array (10 ).fill (0 );
236+ for (let y = x; y; y = Math .floor (y / 10 )) {
237+ ++ cnt1[y % 10 ];
238+ }
239+ let ok = true ;
240+ for (let i = 0 ; i < 10 && ok; ++ i) {
241+ ok = cnt[i] >= cnt1[i];
242+ }
243+ if (ok) {
244+ ans .push (x);
245+ }
245246 }
246- return true ;
247- }
247+ return ans ;
248+ };
248249```
249250
250251<!-- tabs: end -->
0 commit comments