Plus One
Problem Statement
Section titled “Problem Statement”Given a large integer represented as an array of digits, increment it by one.
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Plus One
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Iterate | O(n) | O(1) | When applicable |
| Recursive | O(n) | O(1) | When applicable |
| Math | O(n) | O(1) | When applicable |
Approach 1: Iterate
Section titled “Approach 1: Iterate”This approach provides an efficient solution for plus one.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”from typing import List
def plus_one_iterate(digits: List[int]) -> List[int]: """Increment number by one using iteration.""" carry = 1 for i in range(len(digits) - 1, -1, -1): digits[i] += carry if digits[i] < 10: return digits digits[i] = 0
return [1] + digits
# Test casesprint(plus_one_iterate([1, 2, 3])) # [1, 2, 4]print(plus_one_iterate([9, 9, 9])) # [1, 0, 0, 0]#include <vector>using namespace std;
vector<int> plusOneIterate(vector<int>& digits) { int carry = 1; for (int i = digits.size() - 1; i >= 0; i--) { digits[i] += carry; if (digits[i] < 10) { return digits; } digits[i] = 0; }
digits.insert(digits.begin(), 1); return digits;}
int main() { vector<int> digits = {1, 2, 3}; vector<int> result = plusOneIterate(digits); return 0;}import java.util.Arrays;
public class PlusOneIterate { public static int[] plusOneIterate(int[] digits) { int carry = 1; for (int i = digits.length - 1; i >= 0; i--) { digits[i] += carry; if (digits[i] < 10) { return digits; } digits[i] = 0; }
int[] result = new int[digits.length + 1]; result[0] = 1; return result; }
public static void main(String[] args) { int[] digits = {1, 2, 3}; System.out.println(Arrays.toString(plusOneIterate(digits))); }}function plusOneIterate(digits) { let carry = 1; for (let i = digits.length - 1; i >= 0; i--) { digits[i] += carry; if (digits[i] < 10) { return digits; } digits[i] = 0; }
return [1, ...digits];}
console.log(plusOneIterate([1, 2, 3])); // [1, 2, 4]console.log(plusOneIterate([9, 9, 9])); // [1, 0, 0, 0]fn plus_one_iterate(mut digits: Vec<i32>) -> Vec<i32> { let mut carry = 1; for i in (0..digits.len()).rev() { digits[i] += carry; if digits[i] < 10 { return digits; } digits[i] = 0; }
let mut result = vec![1]; result.extend(digits); result}
fn main() { println!("{:?}", plus_one_iterate(vec![1, 2, 3])); // [1, 2, 4] println!("{:?}", plus_one_iterate(vec![9, 9, 9])); // [1, 0, 0, 0]}package main
import "fmt"
func plusOneIterate(digits []int) []int { carry := 1 for i := len(digits) - 1; i >= 0; i-- { digits[i] += carry if digits[i] < 10 { return digits } digits[i] = 0 }
result := make([]int, len(digits)+1) result[0] = 1 copy(result[1:], digits) return result}
func main() { fmt.Println(plusOneIterate([]int{1, 2, 3})) // [1 2 4] fmt.Println(plusOneIterate([]int{9, 9, 9})) // [1 0 0 0]}Approach 2: Recursive
Section titled “Approach 2: Recursive”This approach provides an efficient solution for plus one.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": pass# Java Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: plus-one# Approach: recursive
# Implementation placeholder for plus_one_recursive# This file is auto-generated and should contain the solution code.
def plus_one_recursive(): pass
if __name__ == "__main__": passApproach 3: Math
Section titled “Approach 3: Math”This approach provides an efficient solution for plus one.
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass# Java Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: plus-one# Approach: math
# Implementation placeholder for plus_one_math# This file is auto-generated and should contain the solution code.
def plus_one_math(): pass
if __name__ == "__main__": pass