Skip to content

Instantly share code, notes, and snippets.

@r14152
Created August 12, 2021 18:20
Show Gist options
  • Save r14152/5b1516078f6670b9a616d56729abf453 to your computer and use it in GitHub Desktop.
Save r14152/5b1516078f6670b9a616d56729abf453 to your computer and use it in GitHub Desktop.
leetcode plusone problem solution
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