您的位置:首页 > 其它

leedcode--Excel Sheet Column Number

2016-03-22 13:40 411 查看
Related to question Excel Sheet Column Title

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



解题思路:进制的转换,26进制转10进制,注意点就是A-Z不是以0开头,所以要记得加1。

java版:

public class Solution {
public int titleToNumber(String s) {

int res=0;
for(int i=0;i<s.length();i++){
res=res*26+(s.charAt[i]-'A'+1);//得到每一位进行减A,然后加1
}
}
return res;
}


c++:

class Solution {
public:
int titleToNumber(string s) {
int res=0;
for (int i=0;i<s.length();i++){
res=res*26+(s[i]-'A'+1);//和java有所不同的是,c++可以直接得到字符串的每一位。
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leedcode