Post

Leetcode - 66. Plus One

Leetcode - 66. Plus One

Hits

  • 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func plusOne(digits []int) []int {
    ln := len(digits)
    rem := 1
    for index:=ln-1; index>=0; index--{
        totalSumofLastIndex := digits[index]+1
        if totalSumofLastIndex<10{
            digits[index]=totalSumofLastIndex
            return digits
        }else{
            digits[index]=totalSumofLastIndex%10
            rem = totalSumofLastIndex/10

        }
    }
    return append([]int{rem}, digits...)
}

This post is licensed under CC BY 4.0 by the author.