Finding 3-Digit Even Numbers LeetCode solution in C++

1
// Runtime: 4 ms, faster than 100.00% of C++ online submissions for Finding
2
// 3-Digit Even Numbers. Memory Usage: 9 MB, less than 100.00% of C++ online
3
// submissions for Finding 3-Digit Even Numbers.
4
class Solution {
5
public:
6
vector<int> findEvenNumbers(vector<int> &digits) {
7
vector<int> ans;
8
int A[10] = {0}; // Array for 10 digits (0,1,2,3...)
9

10
for (auto &x : digits) A[x]++; // Fill our array
11

12
for (int i = 100; i < 999; i += 2) // Look through only correct numbers
13
{
14
int d1 = i / 100, d2 = i / 10 % 10, d3 = i % 10;
15
if (--A[d1] >= 0 & --A[d2] >= 0 & --A[d3] >= 0)
16
ans.push_back(i); // Here we do 2 actions at the same time(we decrease all digits
17
// from our array A and also we check if after the decreasing we
18
// have enough digits in our array)
19
A[d1]++, A[d2]++, A[d3]++; // Refresh our array to start position
20
}
21

22
return ans;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0