From c5bcb82ce776d74dcc8b99a0de12ce133d08652b Mon Sep 17 00:00:00 2001 From: Danyil-Mykola Obertan <45600794+danyaobertan@users.noreply.github.com> Date: Mon, 5 Feb 2024 21:38:01 +0200 Subject: [PATCH] optimized and simplified --- leetcode/0066.Plus-One/66. Plus One.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/leetcode/0066.Plus-One/66. Plus One.go b/leetcode/0066.Plus-One/66. Plus One.go index a396cf45f..9a58ea973 100644 --- a/leetcode/0066.Plus-One/66. Plus One.go +++ b/leetcode/0066.Plus-One/66. Plus One.go @@ -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...) }