The title
ColumnTitle you are given a string columnTitle that represents the column name in an Excel table. Returns the column number corresponding to the column name.
For example, A -> 1 B -> 2 C -> 3… Z -> 26 AA -> 27 AB -> 28 …
Example 1: Input: columnTitle = "A" Output: 1 Example 2: Input: columnTitle = "AB" Output: 28 Example 3: Input: columnTitle = "ZY" Output: 701 Example 4: Input: ColumnTitle = "FXSHRXW" Output: 2147483647Copy the code
Tip:
1 <= columnTitle. Length <= 7 columnTitle Consists of uppercase English only columnTitle is in the range [“A”, “FXSHRXW”]
Their thinking
//import Foundation class Solution { func titleToNumber(_ columnTitle: String) -> Int {var number = 0 // var strList = columntitle.utf8cString // strlist.poplast () // // Var count = 0 // for (_,value) in strlist.enumerated ().reversed() {// // Real numbers // let num = Int(value-64) // Print (count) //// // number = number + num * Int(pow(26,Double(count))) // count += 1 //// Print (number) //} // return number // Int(char.unicodeScalars.first! .value - 64) res = res * 26 + num print(res) } return res } } let columnTitle1 = "AAB" let columnTitle2 = "B" let res = Solution().titleToNumber(columnTitle1) print("res:\(res)")Copy the code