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.
Example 1:
Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
Example 2:
Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
Example 3:
Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
Constraints:
1 <= digits.length <= 100
0 <= digits[i] <= 9
digits
does not contain any leading 0
's.program main
implicit none
integer, parameter :: n = 100
integer(kind=8) :: digits(n), result(n)
! Example 1
digits = [1, 2, 3]
call increment_large_integer(digits, result)
print "(A,*(I1))", "Example 1: ", result
! Example 2
digits = [4, 3, 2, 1]
call increment_large_integer(digits, result)
print "(A,*(I1))", "Example 2: ", result
! Example 3
digits = [9]
call increment_large_integer(digits, result)
print "(A,*(I1))", "Example 3: ", result
contains
subroutine increment_large_integer(digits, result)
implicit none
integer, intent(in) :: digits(:)
integer, intent(out) :: result(:)
integer :: i, carry
carry = 1
do i = size(digits), 1, -1
if (carry == 0) exit
result(i) = digits(i) + carry
carry = 0
if (result(i) >= 10) then
result(i) = result(i) - 10
carry = 1
end if
end do
if (carry /= 0) then
result(1:size(digits)) = digits(1:size(digits))
result(size(digits)+1) = carry
end if
end subroutine increment_large_integer
end program main
temp.f95:8:4: 8 | digits = [1, 2, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 3) temp.f95:9:48: 9 | call increment_large_integer(digits, result) | 1 Error: Type mismatch in argument ‘digits’ at (1); passed INTEGER(8) to INTEGER(4) temp.f95:13:4: 13 | digits = [4, 3, 2, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 4) temp.f95:14:48: 14 | call increment_large_integer(digits, result) | 1 Error: Type mismatch in argument ‘digits’ at (1); passed INTEGER(8) to INTEGER(4) temp.f95:18:4: 18 | digits = [9] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 1) temp.f95:19:48: 19 | call increment_large_integer(digits, result) | 1 Error: Type mismatch in argument ‘digits’ at (1); passed INTEGER(8) to INTEGER(4)
module IncrementLargeInteger
implicit none
private
public :: increment_large_integer
contains
function increment_large_integer(digits) result(result_digits)
integer, intent(in) :: digits(:)
integer :: result_digits(size(digits))
integer :: carry
! Initialize carry to 1
carry = 1
! Iterate over the digits in reverse order
do i = size(digits), 1, -1
! Add the current digit to the carry
result_digits(i) = digits(i) + carry
! If the result is greater than 9, subtract 9 and set carry to 1
if (result_digits(i) > 9) then
result_digits(i) = result_digits(i) - 9
carry = 1
! If the result is less than or equal to 9, set carry to 0
else
carry = 0
end if
end do
! If the carry is still 1 after iterating over all digits,
! add a new digit to the front of the result
if (carry == 1) then
allocate(result_digits(size(digits) + 1))
result_digits(1) = 1
result_digits(2:) = digits
end if
end function increment_large_integer
end module IncrementLargeInteger
program test_increment_large_integer
use IncrementLargeInteger
implicit none
integer, parameter :: example_1_digits(3) = [1, 2, 3]
integer, parameter :: example_1_result(3) = [1, 2, 4]
integer, parameter :: example_2_digits(4) = [4, 3, 2, 1]
integer, parameter :: example_2_result(4) = [4, 3, 2, 2]
integer, parameter :: example_3_digits(1) = [9]
integer, parameter :: example_3_result(2) = [1, 0]
integer :: digits(100)
integer :: i
digits = example_1_digits
write (*, '(A, I0)') 'Example 1: ', increment_large_integer(digits)
do i = 1, size(digits)
write (*, '(I0)', advance='no') digits(i)
end do
write (*, *)
digits = example_2_digits
write (*, '(A, I0)') 'Example 2: ', increment_large_integer(digits)
do i = 1, size(digits)
write (*, '(I0)', advance='no') digits(i)
end do
write (*, *)
digits = example_3_digits
write (*, '(A, I0)') 'Example 3: ', increment_large_integer(digits)
do i = 1, size(digits)
write (*, '(I0)', advance='no') digits(i)
end do
write (*, *)
end program test_increment_large_integer
temp.f95:15:12: 15 | do i = size(digits), 1, -1 | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:32:21: 32 | allocate(result_digits(size(digits) + 1)) | 1 Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER temp.f95:40:9: 40 | use IncrementLargeInteger | 1 Fatal Error: Cannot open module file ‘incrementlargeinteger.mod’ for reading at (1): No such file or directory compilation terminated.
def plusOne(digits: List[int]) -> List[int]:
n = len(digits)
for i in range(n - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
digits.insert(0, 1)
return digits
We start by iterating through the array from the least significant digit (rightmost) to the most significant digit (leftmost). In each iteration, we check whether the digit is less than 9. If the digit is less than 9, increment it and return the incremented array. If the digit is 9, we set it to 0 and continue the loop to the next most significant digit.
After the loop, if we haven't found any digit less than 9, that means we have a number like 9999...9 which needs to be incremented to 10000...0. So we add a 1 at the beginning of the array (the most significant digit). Finally, we return the incremented array.
#include <vector>
std::vector<int> plusOne(std::vector<int>& digits) {
int n = digits.size();
for(int i = n - 1; i >= 0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
digits.insert(digits.begin(), 1);
return digits;
}
We start by iterating through the array from the least significant digit (rightmost) to the most significant digit (leftmost). In each iteration, we check whether the digit is less than 9. If the digit is less than 9, increment it and return the incremented array. If the digit is 9, we set it to 0 and continue the loop to the next most significant digit.
After the loop, if we haven't found any digit less than 9, that means we have a number like 9999...9 which needs to be incremented to 10000...0. So we add a 1 at the beginning of the array (the most significant digit). Finally, we return the incremented array.