Leetcode Note: Go - Add to Array Form of Integer

Add to Array-Form of Integer - LeetCode
https://leetcode.com/problems/add-to-array-form-of-integer/

  • Go 言語で取り組んだメモ

回答

Add to Array-Form of Integer - LeetCode
https://leetcode.com/problems/add-to-array-form-of-integer/solution/

GO Solution - LeetCode Discuss
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/234831/GO-Solution

func addToArrayForm(A []int, K int) []int {
    carry:=0
    digK:=0
    res:=[]int{}      
    for i:=len(A)-1;i>=0;i-- {
        digK=K%10
        K=K/10
        res=append(res,(A[i]+digK+carry)%10)
        carry=(A[i]+digK+carry)/10
    }

     for K>0 {
		digK=K%10
		K=K/10		
        res=append(res,(digK+carry)%10)
		carry=(digK+carry)/10		
    }
    if carry>0 {
        res=append(res,carry)
    } 
    for i:=0;i<len(res)/2;i++ {
        tmp:=res[i]
        res[i]=res[len(res)-1-i]
        res[len(res)-1-i]=tmp
    }    
    return res        
}