@@ -73,32 +73,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Un
73
73
74
74
<!-- solution:start -->
75
75
76
- ### Solution 1
76
+ ### Solution 1: Hash Set + Enumeration
77
+
78
+ We use a hash set $\textit{s}$ to record all distinct three-digit even numbers, and then enumerate all possible three-digit even numbers to add them to the hash set.
79
+
80
+ Finally, we return the size of the hash set.
81
+
82
+ The time complexity is $O(n^3)$, and the space complexity is $O(n^3)$. Where $n$ is the length of the array $\textit{digits}$.
77
83
78
84
<!-- tabs:start -->
79
85
80
86
#### Python3
81
87
82
88
``` python
83
-
89
+ class Solution :
90
+ def totalNumbers (self , digits : List[int ]) -> int :
91
+ s = set ()
92
+ for i, a in enumerate (digits):
93
+ if a & 1 :
94
+ continue
95
+ for j, b in enumerate (digits):
96
+ if i == j:
97
+ continue
98
+ for k, c in enumerate (digits):
99
+ if c == 0 or k in (i, j):
100
+ continue
101
+ s.add(c * 100 + b * 10 + a)
102
+ return len (s)
84
103
```
85
104
86
105
#### Java
87
106
88
107
``` java
89
-
108
+ class Solution {
109
+ public int totalNumbers (int [] digits ) {
110
+ Set<Integer > s = new HashSet<> ();
111
+ int n = digits. length;
112
+ for (int i = 0 ; i < n; ++ i) {
113
+ if (digits[i] % 2 == 1 ) {
114
+ continue ;
115
+ }
116
+ for (int j = 0 ; j < n; ++ j) {
117
+ if (i == j) {
118
+ continue ;
119
+ }
120
+ for (int k = 0 ; k < n; ++ k) {
121
+ if (digits[k] == 0 || k == i || k == j) {
122
+ continue ;
123
+ }
124
+ s. add(digits[k] * 100 + digits[j] * 10 + digits[i]);
125
+ }
126
+ }
127
+ }
128
+ return s. size();
129
+ }
130
+ }
90
131
```
91
132
92
133
#### C++
93
134
94
135
``` cpp
95
-
136
+ class Solution {
137
+ public:
138
+ int totalNumbers(vector<int >& digits) {
139
+ unordered_set<int > s;
140
+ int n = digits.size();
141
+ for (int i = 0; i < n; ++i) {
142
+ if (digits[ i] % 2 == 1) {
143
+ continue;
144
+ }
145
+ for (int j = 0; j < n; ++j) {
146
+ if (i == j) {
147
+ continue;
148
+ }
149
+ for (int k = 0; k < n; ++k) {
150
+ if (digits[ k] == 0 || k == i || k == j) {
151
+ continue;
152
+ }
153
+ s.insert(digits[ k] * 100 + digits[ j] * 10 + digits[ i] );
154
+ }
155
+ }
156
+ }
157
+ return s.size();
158
+ }
159
+ };
96
160
```
97
161
98
162
#### Go
99
163
100
164
```go
165
+ func totalNumbers(digits []int) int {
166
+ s := make(map[int]struct{})
167
+ for i, a := range digits {
168
+ if a%2 == 1 {
169
+ continue
170
+ }
171
+ for j, b := range digits {
172
+ if i == j {
173
+ continue
174
+ }
175
+ for k, c := range digits {
176
+ if c == 0 || k == i || k == j {
177
+ continue
178
+ }
179
+ s[c*100+b*10+a] = struct{}{}
180
+ }
181
+ }
182
+ }
183
+ return len(s)
184
+ }
185
+ ```
101
186
187
+ #### TypeScript
188
+
189
+ ``` ts
190
+ function totalNumbers(digits : number []): number {
191
+ const s = new Set <number >();
192
+ const n = digits .length ;
193
+ for (let i = 0 ; i < n ; ++ i ) {
194
+ if (digits [i ] % 2 === 1 ) {
195
+ continue ;
196
+ }
197
+ for (let j = 0 ; j < n ; ++ j ) {
198
+ if (i === j ) {
199
+ continue ;
200
+ }
201
+ for (let k = 0 ; k < n ; ++ k ) {
202
+ if (digits [k ] === 0 || k === i || k === j ) {
203
+ continue ;
204
+ }
205
+ s .add (digits [k ] * 100 + digits [j ] * 10 + digits [i ]);
206
+ }
207
+ }
208
+ }
209
+ return s .size ;
210
+ }
102
211
```
103
212
104
213
<!-- tabs: end -->
0 commit comments