66. Plus One
LeetCode 66. Plus One
Description
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.Tags
Solution
Complexity
Code
Last updated
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.Last updated
func plusOne(digits []int) []int {
c := 1
for i := len(digits) - 1; i >= 0; i-- {
digits[i] += c
c = 0 // important
if digits[i] > 9 {
c = digits[i] / 10
digits[i] %= 10
}
}
if c != 0 {
return append([]int{c}, digits...)
}
return digits
}