Created
August 12, 2021 18:20
-
-
Save r14152/5b1516078f6670b9a616d56729abf453 to your computer and use it in GitHub Desktop.
leetcode plusone problem solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func plusOne(digits []int) []int { | |
length := len(digits)-1 | |
var remain int | |
firstSum := true | |
for j:= length;j>=0;j--{ | |
var sum int | |
if firstSum { | |
sum = digits[j]+1 | |
firstSum = false | |
}else{ | |
sum = digits[j]+remain | |
} | |
if sum> 9 { | |
digits[j] = 0 | |
remain = 1 | |
continue | |
} | |
digits[j] = sum | |
remain = 0 | |
} | |
if remain> 0 { | |
digits = append([]int{remain}, digits...) | |
} | |
return digits | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment