Merge pull request #281 from danyaobertan/patch-1

optimized and simplified
This commit is contained in:
halfrost
2024-12-10 21:55:51 -08:00
committed by GitHub

View File

@ -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...)
}