Swift, need converting functions


  • 0
    X

    It is not like in C++, we need to use Character(UnicodeScalar(65)) to get char c = 'A'; and also str.insert(c, at:str.startIndex) to do str = c + str;

    class Solution {
        func convertToTitle(_ n: Int) -> String {
            
            // get string from char by ASCII:
            // var string = ""
            // string.append(Character(UnicodeScalar(65)))
            // convert char to int:
            // let startingValue = Int(("A" as UnicodeScalar).value) // 65
            
            var result : String = ""
            var num = n
            while(num > 0){
                num -= 1
                let lower = num % 26
                let c = Character(UnicodeScalar(65 + lower)!)
                result.insert(c, at:result.startIndex)
                num /= 26
            }
            return result
        }
    }
    

Log in to reply
 

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