mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
Merge pull request #281 from danyaobertan/patch-1
optimized and simplified
This commit is contained in:
@ -2,16 +2,11 @@ package leetcode
|
||||
|
||||
func plusOne(digits []int) []int {
|
||||
for i := len(digits) - 1; i >= 0; i-- {
|
||||
digits[i]++
|
||||
if digits[i] != 10 {
|
||||
// no carry
|
||||
if digits[i] != 9 {
|
||||
digits[i]++
|
||||
return digits
|
||||
}
|
||||
// carry
|
||||
digits[i] = 0
|
||||
}
|
||||
// all carry
|
||||
digits[0] = 1
|
||||
digits = append(digits, 0)
|
||||
return digits
|
||||
return append([]int{1}, digits...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user