My simple swift solution


  • 0
    Y
    class Solution {
        func convertToBase7(_ num: Int) -> String {
            var input:Int = (num > 0) ? num : num * -1
            var remainder = input % 7
            var ans:String = "\(remainder)"
            
            while input >= 7{
                input -= remainder
                input = input/7
                remainder = input % 7
                ans.append(input < 7 ? "\(input)" : "\(remainder)")
            }
            return (num >= 0) ? String(ans.characters.reversed()) : "-" + String(ans.characters.reversed())
        }
    }

Log in to reply
 

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