【LeetCode】#168 Excel Sheet Column Title(C#)

● 問題

Given a positive integer, return its corresponding column title as appear in an Excel sheet.


● 思考點

要先了解ASCII所對應的字元,而我們不可能預先知道A的ASCII,所以把A當作基準點,接著轉換成ASCII比較妥當。


● Solution

public class Solution {
    public string ConvertToTitle(int n) {
    
        var  result = "";
        
        while(n > 0)
        {
            n--;
            result = Convert.ToChar((n % 26) + 'A') + result;
            n /= 26;
        }
        
        return result;
    }
}