Skip to content

Commit ee1f160

Browse files
authored
feat: add rust solution to lc problem: No.2670 (doocs#1098)
1 parent 521eab6 commit ee1f160

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

solution/2600-2699/2670.Find the Distinct Difference Array/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,64 @@ function distinctDifferenceArray(nums: number[]): number[] {
182182
}
183183
```
184184

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+
185243
### **...**
186244

187245
```

solution/2600-2699/2670.Find the Distinct Difference Array/README_EN.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,64 @@ function distinctDifferenceArray(nums: number[]): number[] {
170170
}
171171
```
172172

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+
173231
### **...**
174232

175233
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)