Monday, May 11, 2015

168 Excel Sheet Column Title

来源:Leetcode

原帖:https://oj.leetcode.com/problems/excel-sheet-column-title/

题目:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB

代码:
 class Solution {  
 public:  
   string convertToTitle(int i) {  
     string res;  
     while (i) {  
       char c = (i - 1) % 26 + 'A';  
       res.push_back(c);  
       i = (i - 1) / 26;  
     }  
     reverse(res.begin(), res.end());  
     return res;  
   }    
 };  

No comments:

Post a Comment