Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 883 Bytes

leetcode_989.md

File metadata and controls

34 lines (26 loc) · 883 Bytes

LeetCode Problems

997. Add to Array-Form of Integer

class Solution {
    fun addToArrayForm(num: IntArray, k: Int): List<Int> {
        var result = mutableListOf<Int>()
        var pointer = num.size
        var kNum = k
        // iterate with moving pointer
        while (--pointer >= 0 || kNum > 0) {
            // if pointer is possible to move, add current index-number
            if (pointer >= 0) kNum += num[pointer]
            // add current summation's index number to answer list
            result.add(kNum % 10)
            // modular calculation for next iteration
            kNum /= 10
        }
        // return reversed array
        return result.reversed() 
    }
}