[LeetCode][Java] Excel Sheet Column Number


質問する
Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number.

せいげんじょうけん
  • 1 ≤\leq≤ columnTitle.length ≤\leq≤ 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"] .
  • に近づくZA、すなわち26進法を仮定して近似解を行う.また,それぞれの数字はAZの数字が存在するように近接しており,簡単な解法が行われている.
    答案用紙
    class Solution {
        
        public int titleToNumber(String columnTitle) {
            int answer = 0;
    
            for(int i = columnTitle.length() - 1, radix = 1; i >= 0; --i, radix *= (int)('Z' - 'A' + 1)){
                answer += (int)(columnTitle.charAt(i) - 'A' + 1) * radix;
            }
    
            return answer;
        }
    }