mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
optimized and simplified
This commit is contained in:

committed by
GitHub

parent
3200de1f73
commit
c5bcb82ce7
@ -2,16 +2,11 @@ package leetcode
|
|||||||
|
|
||||||
func plusOne(digits []int) []int {
|
func plusOne(digits []int) []int {
|
||||||
for i := len(digits) - 1; i >= 0; i-- {
|
for i := len(digits) - 1; i >= 0; i-- {
|
||||||
|
if digits[i] != 9 {
|
||||||
digits[i]++
|
digits[i]++
|
||||||
if digits[i] != 10 {
|
|
||||||
// no carry
|
|
||||||
return digits
|
return digits
|
||||||
}
|
}
|
||||||
// carry
|
|
||||||
digits[i] = 0
|
digits[i] = 0
|
||||||
}
|
}
|
||||||
// all carry
|
return append([]int{1}, digits...)
|
||||||
digits[0] = 1
|
|
||||||
digits = append(digits, 0)
|
|
||||||
return digits
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user