-
-
Save rust-play/300f877e9075b2197fe5d1bcd1fae8a1 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn find_subseq(s: &[u8]) -> u8 { | |
let mut res: u8 = 1; | |
let mut i = 0; | |
while (i < s.len() - 1) && s[i] < 255 && (s[i] + 1 == s[i + 1]) { | |
res += 1; | |
i += 1; | |
} | |
return res; | |
} | |
pub fn longest_incrementing_subslice(s: &[u8]) -> &[u8] { | |
let mut res = (0, 1); | |
let mut el = 0; | |
while el < s.len() - 1 { | |
let subl = find_subseq(&s[el..s.len()]); | |
if subl > res.1 { | |
res.0 = el; | |
res.1 = subl; | |
} | |
el = el + subl as usize; | |
} | |
return &s[res.0..res.0 + res.1 as usize]; | |
} | |
fn main() { | |
let data = &[2, 5, 2, 253, 254, 255, 1]; | |
let result = longest_incrementing_subslice(data); | |
println!("{:?}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment