File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed
solution/2600-2699/2670.Find the Distinct Difference Array Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change @@ -182,6 +182,64 @@ function distinctDifferenceArray(nums: number[]): number[] {
182
182
}
183
183
```
184
184
185
+ ### ** Rust**
186
+
187
+ ``` rust
188
+ use std :: collections :: HashSet ;
189
+
190
+ impl Solution {
191
+ pub fn distinct_difference_array (nums : Vec <i32 >) -> Vec <i32 > {
192
+ let mut ans : Vec <i32 > = Vec :: new ();
193
+
194
+ for i in 0 .. nums . len () {
195
+ let mut j = 0 ;
196
+ let mut hash1 = HashSet :: new ();
197
+ while j <= i {
198
+ hash1 . insert (nums [j ]);
199
+ j += 1 ;
200
+ }
201
+
202
+ let mut k = i + 1 ;
203
+ let mut hash2 = HashSet :: new ();
204
+ while k < nums . len () {
205
+ hash2 . insert (nums [k ]);
206
+ k += 1 ;
207
+ }
208
+
209
+ ans . push ((hash1 . len () - hash2 . len ()) as i32 );
210
+ }
211
+
212
+ ans
213
+ }
214
+ }
215
+ ```
216
+
217
+ ``` rust
218
+ use std :: collections :: HashSet ;
219
+
220
+ impl Solution {
221
+ pub fn distinct_difference_array (nums : Vec <i32 >) -> Vec <i32 > {
222
+ let n = nums . len ();
223
+ let mut s = vec! [0 ; n + 1 ];
224
+ let mut set = HashSet :: new ();
225
+
226
+ for i in (0 .. n ). rev () {
227
+ set . insert (nums [i ]);
228
+ s [i ] = set . len ();
229
+ }
230
+
231
+ let mut ans = Vec :: new ();
232
+ set . clear ();
233
+ for i in 0 .. n {
234
+ set . insert (nums [i ]);
235
+ ans . push ((set . len () - s [i + 1 ]) as i32 );
236
+ }
237
+
238
+ ans
239
+ }
240
+ }
241
+ ```
242
+
185
243
### ** ...**
186
244
187
245
```
Original file line number Diff line number Diff line change @@ -170,6 +170,64 @@ function distinctDifferenceArray(nums: number[]): number[] {
170
170
}
171
171
```
172
172
173
+ ### ** Rust**
174
+
175
+ ``` rust
176
+ use std :: collections :: HashSet ;
177
+
178
+ impl Solution {
179
+ pub fn distinct_difference_array (nums : Vec <i32 >) -> Vec <i32 > {
180
+ let mut ans : Vec <i32 > = Vec :: new ();
181
+
182
+ for i in 0 .. nums . len () {
183
+ let mut j = 0 ;
184
+ let mut hash1 = HashSet :: new ();
185
+ while j <= i {
186
+ hash1 . insert (nums [j ]);
187
+ j += 1 ;
188
+ }
189
+
190
+ let mut k = i + 1 ;
191
+ let mut hash2 = HashSet :: new ();
192
+ while k < nums . len () {
193
+ hash2 . insert (nums [k ]);
194
+ k += 1 ;
195
+ }
196
+
197
+ ans . push ((hash1 . len () - hash2 . len ()) as i32 );
198
+ }
199
+
200
+ ans
201
+ }
202
+ }
203
+ ```
204
+
205
+ ``` rust
206
+ use std :: collections :: HashSet ;
207
+
208
+ impl Solution {
209
+ pub fn distinct_difference_array (nums : Vec <i32 >) -> Vec <i32 > {
210
+ let n = nums . len ();
211
+ let mut s = vec! [0 ; n + 1 ];
212
+ let mut set = HashSet :: new ();
213
+
214
+ for i in (0 .. n ). rev () {
215
+ set . insert (nums [i ]);
216
+ s [i ] = set . len ();
217
+ }
218
+
219
+ let mut ans = Vec :: new ();
220
+ set . clear ();
221
+ for i in 0 .. n {
222
+ set . insert (nums [i ]);
223
+ ans . push ((set . len () - s [i + 1 ]) as i32 );
224
+ }
225
+
226
+ ans
227
+ }
228
+ }
229
+ ```
230
+
173
231
### ** ...**
174
232
175
233
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ impl Solution {
4
+ pub fn distinct_difference_array ( nums : Vec < i32 > ) -> Vec < i32 > {
5
+ let n = nums. len ( ) ;
6
+ let mut s = vec ! [ 0 ; n + 1 ] ;
7
+ let mut set = HashSet :: new ( ) ;
8
+
9
+ for i in ( 0 ..n) . rev ( ) {
10
+ set. insert ( nums[ i] ) ;
11
+ s[ i] = set. len ( ) ;
12
+ }
13
+
14
+ let mut ans = Vec :: new ( ) ;
15
+ set. clear ( ) ;
16
+ for i in 0 ..n {
17
+ set. insert ( nums[ i] ) ;
18
+ ans. push ( ( set. len ( ) - s[ i + 1 ] ) as i32 ) ;
19
+ }
20
+
21
+ ans
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments