optimized and simplified

This commit is contained in:
Danyil-Mykola Obertan
2024-02-05 21:38:01 +02:00
committed by GitHub
parent 3200de1f73
commit c5bcb82ce7

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