Leetcode 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 

 1 public class Solution {
 2     public string ConvertToTitle(int n) {
 3         var sb = new StringBuilder();
 4         
 5         while (n > 0)
 6         {
 7             var up = (n - 1) / 26;
 8             var left = (n - 1) % 26;
 9             
10             sb.Insert(0, (char)(left + (int)'A'));
11             n = up;
12         }
13         
14         return sb.ToString();
15     }
16 }

 

posted @ 2017-11-23 08:20  逸朵  阅读(133)  评论(0)    收藏  举报