Golang刷題Leetcode 66. Plus One

題目:Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

給一個非空數組,進行加一操作

例如

輸入:[1,2,3]

輸出:[1,2,4]

思路

簡單題目,只要注意處理進位的情況就可以了

code

func plusOne(digits []int) []int {
l := len(digits)
for i := l - 1; i >= 0; i-- {
if digits[i] < 9 {
digits[i]++
return digits
}
digits[i] = 0
}
//如果有進位
res := []int{1}
res = append(res, digits...)
return res
}

更多內容請移步我的repo:https://github.com/anakin/golang-leetcode


分享到:


相關文章: