Skip to content

Commit

Permalink
Merge pull request #281 from danyaobertan/patch-1
Browse files Browse the repository at this point in the history
optimized and simplified
  • Loading branch information
halfrost authored Dec 11, 2024
2 parents a190dbe + c5bcb82 commit d78a9e0
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions leetcode/0066.Plus-One/66. Plus One.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

0 comments on commit d78a9e0

Please sign in to comment.