File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
solution/0100-0199/0128.Longest Consecutive Sequence Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -195,6 +195,39 @@ public:
195
195
};
196
196
```
197
197
198
+ ### ** Rust**
199
+
200
+ ``` rust
201
+ use std :: collections :: HashSet ;
202
+
203
+ impl Solution {
204
+ #[allow(dead_code)]
205
+ pub fn longest_consecutive (nums : Vec <i32 >) -> i32 {
206
+ let mut s = HashSet :: new ();
207
+ let mut ret = 0 ;
208
+
209
+ // Initialize the set
210
+ for num in & nums {
211
+ s . insert (* num );
212
+ }
213
+
214
+ for num in & nums {
215
+ if s . contains (& (* num - 1 )) {
216
+ continue ;
217
+ }
218
+ let mut cur_num = num . clone ();
219
+ while s . contains (& cur_num ) {
220
+ cur_num += 1 ;
221
+ }
222
+ // Update the answer
223
+ ret = std :: cmp :: max (ret , cur_num - num );
224
+ }
225
+
226
+ ret
227
+ }
228
+ }
229
+ ```
230
+
198
231
### ** Go**
199
232
200
233
``` go
Original file line number Diff line number Diff line change @@ -186,6 +186,39 @@ public:
186
186
};
187
187
```
188
188
189
+ ### ** Rust**
190
+
191
+ ``` rust
192
+ use std :: collections :: HashSet ;
193
+
194
+ impl Solution {
195
+ #[allow(dead_code)]
196
+ pub fn longest_consecutive (nums : Vec <i32 >) -> i32 {
197
+ let mut s = HashSet :: new ();
198
+ let mut ret = 0 ;
199
+
200
+ // Initialize the set
201
+ for num in & nums {
202
+ s . insert (* num );
203
+ }
204
+
205
+ for num in & nums {
206
+ if s . contains (& (* num - 1 )) {
207
+ continue ;
208
+ }
209
+ let mut cur_num = num . clone ();
210
+ while s . contains (& cur_num ) {
211
+ cur_num += 1 ;
212
+ }
213
+ // Update the answer
214
+ ret = std :: cmp :: max (ret , cur_num - num );
215
+ }
216
+
217
+ ret
218
+ }
219
+ }
220
+ ```
221
+
189
222
### ** Go**
190
223
191
224
``` go
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ impl Solution {
4
+ #[ allow( dead_code) ]
5
+ pub fn longest_consecutive ( nums : Vec < i32 > ) -> i32 {
6
+ let mut s = HashSet :: new ( ) ;
7
+ let mut ret = 0 ;
8
+
9
+ // Initialize the set
10
+ for num in & nums {
11
+ s. insert ( * num) ;
12
+ }
13
+
14
+ for num in & nums {
15
+ if s. contains ( & ( * num - 1 ) ) {
16
+ continue ;
17
+ }
18
+ let mut cur_num = num. clone ( ) ;
19
+ while s. contains ( & cur_num) {
20
+ cur_num += 1 ;
21
+ }
22
+ // Update the answer
23
+ ret = std:: cmp:: max ( ret, cur_num - num) ;
24
+ }
25
+
26
+ ret
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments