LeetCode No.168 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 

===================================================================

題目鏈接:https://leetcode.com/problems/excel-sheet-column-title/

題目大意:求出用A-Z表示的26進制數。

思路:模擬26進制生成。

參考代碼:

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章