Given a string columnTitle
that represents the column title as appear in an Excel sheet, return its corresponding column number.
columnTitle
consists only of uppercase English letters.columnTitle
is in the range ["A", "FXSHRXW"]
.Z
- A
, 즉 26진법으로 가정하고 접근해서 풀었어요. 그리고 각 한자리 숫자는 A
에서 Z
라는 숫자만 존재한다는 식으로 접근했고 간단하게 풀었어요.
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;
}
}