Skip to content

Instantly share code, notes, and snippets.

@D-Lite
Created January 1, 2026 09:53
Show Gist options
  • Select an option

  • Save D-Lite/02ee37d7a42b504c642b3c2104230fee to your computer and use it in GitHub Desktop.

Select an option

Save D-Lite/02ee37d7a42b504c642b3c2104230fee to your computer and use it in GitHub Desktop.
Jan 1
To handle this leetcode question, I worked on an iterative solution.
- Reversed the initial array (the rust baseline didn't make the array mutable initially)
- Initialized a carry 1
- Iterated from the least significant digit, now at index 0
- I kept moving from left to right, adding 1 if > 9 else add 1 to the first digit
- Reversed the array back.
Pretty easy
https://leetcode.com/problems/plus-one/submissions/1870882523
impl Solution {
pub fn plus_one(mut digits: Vec<i32>) -> Vec<i32> {
digits.reverse();
let (mut one, mut i) = (1, 0);
while one == 1 {
if i < digits.len() {
if digits[i] == 9 {
digits[i] = 0;
} else {
digits[i] += 1;
one = 0;
}
} else {
digits.push(1);
one = 0;
}
i += 1
}
digits.reverse();
digits
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment