Excel Sheet Column Number
https://leetcode.com/problems/excel-sheet-column-number/description/
Related to questionExcel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
Thoughts
26进制转换. 只是每次要多加1因为它要的不是0~25而是1~26.
Code
class Solution {
public int titleToNumber(String s) {
int sum = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
sum += Math.pow(26, s.length() - 1 - i) * (c - 'A' + 1);
}
return sum;
}
}
Analysis
时间复杂度O(N), N为字符串长度. 空间O(1).
Last updated
Was this helpful?