-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplusOne.go
More file actions
35 lines (29 loc) · 843 Bytes
/
Copy pathplusOne.go
File metadata and controls
35 lines (29 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* https://leetcode.com/problems/plus-one/#/description
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
给定一个非负整数,表示为非空数组,然后执行+1运算。
*/
package larray
func plusOne(digits []int) []int {
carry, length := 1, len(digits) // carry default = 1 for units + 1
for i := length - 1; i >= 0; i-- {
cur := digits[i] + carry
if cur == 10 {
carry = 1
digits[i] = 0
} else {
digits[i] = cur
return digits
}
}
if carry == 1 {
r := make([]int, length+1)
r[0] = 1
for i := 1; i < length+1; i++ {
r[i] = digits[i-1]
}
return r
}
return digits
}