【easy】168. Excel Sheet Column Title 171. Excel Sheet Column Number

class Solution {
public:
string convertToTitle(int n) {
if (n == ) {
return "";
}
return convertToTitle((n - ) / ) + (char)((n - ) % + 'A');
}
};
    A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
答案写法:
class Solution {
public:
int titleToNumber(string s) { int sum = ;
int tmp = ;
for (int i = ; i < s.length(); ++i) {
tmp = s[i] - 'A' + ;
sum = * sum + tmp;
}
return sum;
  }
};
下面是自己写的:
class Solution {
public:
int titleToNumber(string s) {
int len = s.length();
int num = ;
if (len == )
num = s[]-'A'+;
else{
num += s[len-]-'A'+;
int cal = ;
for (int i=len-;i>=;i--){
num += (s[i]-'A'+)*cal;
cal *= ;
} }
return num;
}
};//从字符串到数字
上一篇:linux oracle profile配置


下一篇:[WinAPI] API 11 [创建目录]