1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
fn main() {
println!("Hello, world!");
let tests = [
(vec![100,4,200,1,3,2], 4),
(vec![0,3,7,2,5,8,4,6,0,1], 9)
];
for test in tests {
println!("{:?} is {} should be {}", test.0, Solution::longest_consecutive(test.0.clone()), test.1);
}
}
struct Solution;
impl Solution {
pub fn longest_consecutive(nums: Vec<i32>) -> i32 {
let mut s = nums.clone();
s.sort();
s.dedup();
let mut longest = (nums.len() > 0) as i32;
let mut longest_old = 0;
for i in s.windows(2) {
if &i[1] != &(&i[0] + 1) {
if longest > longest_old {
longest_old = longest;
}
longest = 1;
continue;
}
longest += 1;
}
longest.max(longest_old)
}
}
|