#66
Easy Algorithms Plus one
Array Math
49.6% acceptance
Jan 12, 2026
11731
5597
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn plus_one(mut digits: Vec<i32>) -> Vec<i32> {
for i in (0..digits.len()).rev() {
if digits[i] < 9 {
digits[i] += 1;
return digits;
}
digits[i] = 0;
}
// If we reach here, all digits were 9
let mut result = vec![1];
result.extend(digits);
result
}
}