Given a positive integer, return its corresponding column title as appear in an Excel sheet.
class Solution { public: string convertToTitle(int n) { string s; while(n){ s.insert(s.begin(),(n-1)%26+'A');//在字符串起始位插入 n=(n-1)/26; } return s; } };
由于没有0,所以需要先减1
Given a column title as appear in an Excel sheet, return its corresponding column number.
class Solution { public: int titleToNumber(string s) { int rs=0,j,a; for(int i=0;i<s.length();i++){ j=s.length()-i-1; a=s[j]-'A'+1; rs+=(a*pow(26,i)); } return rs; } };
时间: 2024-10-26 14:15:08