Swift solution


  • 0
    T
    class Solution {
        func addDigits(_ num: Int) -> Int {
            var arr1:[String] = []
            var loopNum:Int = 0
            var result:Int = num
    
            for temp in String(result).characters {
                arr1.append(String(temp))
            }
    
            while arr1.count > 1 {
                result = 0
                for temp in 0...(arr1.count - 1) {
                    let myInt:Int? = Int(arr1[loopNum])
                    result += myInt!
                    loopNum+=1
                }  
                loopNum = 0
                arr1.removeAll()
                for temp in String(result).characters {
                    arr1.append(String(temp))
                }
            }
            return result
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.