diff --git a/src/problem/mod.rs b/src/problem/mod.rs index ee13a71..86cf501 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -188,4 +188,5 @@ mod p190_reverse_bits; mod p191_number_of_1_bits; mod p136_single_number; mod p137_single_number_ii; -mod p201_bitwise_and_of_numbers_range; \ No newline at end of file +mod p201_bitwise_and_of_numbers_range; +mod p66_plus_one; \ No newline at end of file diff --git a/src/problem/p66_plus_one.rs b/src/problem/p66_plus_one.rs new file mode 100644 index 0000000..fb57d53 --- /dev/null +++ b/src/problem/p66_plus_one.rs @@ -0,0 +1,38 @@ +/** + * [66] Plus One + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn plus_one(digits: Vec) -> Vec { + let mut digits = digits; + let mut overflow = 1; + + for i in (0..digits.len()).rev() { + let bit = digits[i] + overflow; + digits[i] = bit % 10; + overflow = bit / 10; + } + + if overflow > 0 { + digits.insert(0, overflow); + } + + digits + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_66() { + assert_eq!(vec![1, 0], Solution::plus_one(vec![9])); + } +}