From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- _missing/Cargo.toml | 8 ++++++++ _missing/src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 _missing/Cargo.toml create mode 100644 _missing/src/main.rs (limited to '_missing') diff --git a/_missing/Cargo.toml b/_missing/Cargo.toml new file mode 100644 index 0000000..b3979a3 --- /dev/null +++ b/_missing/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "leetcode" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/_missing/src/main.rs b/_missing/src/main.rs new file mode 100644 index 0000000..4d903d1 --- /dev/null +++ b/_missing/src/main.rs @@ -0,0 +1,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 { + 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) + } +} -- cgit v1.2.3