Algorithm
Problem Name: 66. Plus One
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 leading0
's.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <stdlib.h>
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int *ans = (int *)malloc((digitsSize + 1) * sizeof(int));
*returnSize = digitsSize;
int i;
int sum = 1; /* plus one */
for (i = digitsSize - 1; i >= 0; i--) {
sum += digits[i];
ans[i + 1] = sum % 10;
sum /= 10;
}
if (sum) {
ans[0] = sum;
*returnSize = *returnSize + 1;
return ans;
}
else {
return ans + 1;
}
}
int main() {
int A[] = { 9, 9, 9 };
int retSize = 0;
int *ans = plusOne(A, sizeof(A) / sizeof(A[0]), &retSize);
int i;
for (i = 0; i < retSize; i++) {
printf("%d ", ans[i]);
}
printf("\n");
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size() - 1;
while(i >= 0 && digits[i] == 9) digits[i--] = 0;
if(i < 0) digits.push_back(0>, i++;
digits[i]++;
return digits;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
class Solution {
public int[] plusOne(int[] digits) {
int carry = 1;
int n = digits.length;
for (int i = n - 1; i >= 0; i--) {
int temp = digits[i] + carry;
if (temp < = 9) {
digits[i] = temp;
return digits;
}
digits[i] = temp % 10;
}
int[] newDigits = new int[n + 1];
newDigits[0] = 1;
for (int i = 1; i < n + 1; i++) {
newDigits[i] = digits[i - 1];
}
return newDigits;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const plusOne = function (digits) {
for (let i = digits.length - 1; i >= 0; i--) {
if (digits[i] !== 9) {
digits[i]++
return digits
} else {
digits[i] = 0
}
}
digits.unshift(1)
return digits
}
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def plusOne(self, digits, add = 1):
return add and [1] or [] if not digits else self.plusOne(digits[:-1], +(digits[-1] + add > 9)) + [(digits[-1] + add) % 10]
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with C# Programming
Code -
C# Programming
using System.Collections.Generic;
namespace LeetCode
{
public class _066_PlusOne
{
public int[] PlusOne(int[] digits)
{
var result = new List < int>();
var carry = 1;
var value = 0;
for (int i = digits.Length - 1; i >= 0; i--)
{
value = digits[i] + carry;
carry = value == 10 ? 1 : 0;
result.Insert(0, value % 10);
}
if (carry >= 1)
result.Insert(0, carry);
return result.ToArray();
}
}
}
Copy The Code &
Try With Live Editor
Input
Output