您的位置:首页 > 其它

Leetcode——171. Excel Sheet Column Number

2017-01-21 23:48 399 查看

题目

https://leetcode.com/problems/excel-sheet-column-number/

解答

其实就是26进制转化为十进制

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);
}
return res;
}
};


相似题目:

class Solution {
public:
string convertToTitle(int n) {
string res="";
while(n>0)
{
if(n%26==0)
{
res="Z"+res;
n=n/26-1;
}
else
{
res= (char)(n%26+'A'-1)+res;
n=n/26;
}

}
return res;
}
};


Or

string convertToTitle(int n) {
string ans;
while (n) {
ans = char ((n - 1) % 26 + 'A') + ans;
n = (n - 1) / 26;
}
return ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: