您的位置:首页 > 其它

LeetCode Excel Sheet Column Number

2015-08-14 00:37 330 查看
原题链接在这里:https://leetcode.com/problems/excel-sheet-column-number/

题目:

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

题解:

这道题与Excel Sheet Column Title相呼应。Time O(n), Space O(1).

注意cast时后面的数要加个整体的括号。

AC Java:

public class Solution {
public int titleToNumber(String s) {
if(s == null || s.length() == 0){
return 0;
}
int res = 0;
int scale = 26;
for(int i = 0; i<s.length(); i++){
res = res*scale + (int)(s.charAt(i)-'A')+1;
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: