public class Solution {
    private string Convert(int k)
        {
            var s = "";
            switch (k)
            {
                case 1:
                    s = "A";
                    break;
                case 2:
                    s = "B";
                    break;
                case 3:
                    s = "C";
                    break;
                case 4:
                    s = "D";
                    break;
                case 5:
                    s = "E";
                    break;
                case 6:
                    s = "F";
                    break;
                case 7:
                    s = "G";
                    break;
                case 8:
                    s = "H";
                    break;
                case 9:
                    s = "I";
                    break;
                case 10:
                    s = "J";
                    break;
                case 11:
                    s = "K";
                    break;
                case 12:
                    s = "L";
                    break;
                case 13:
                    s = "M";
                    break;
                case 14:
                    s = "N";
                    break;
                case 15:
                    s = "O";
                    break;
                case 16:
                    s = "P";
                    break;
                case 17:
                    s = "Q";
                    break;
                case 18:
                    s = "R";
                    break;
                case 19:
                    s = "S";
                    break;
                case 20:
                    s = "T";
                    break;
                case 21:
                    s = "U";
                    break;
                case 22:
                    s = "V";
                    break;
                case 23:
                    s = "W";
                    break;
                case 24:
                    s = "X";
                    break;
                case 25:
                    s = "Y";
                    break;
                case 0:
                    s = "Z";
                    break;
            }
            return s;
        }

        public string ConvertToTitle(int n)
        {
            var list = new List<int>();
            while (n != 0)
            {
                var k = n % 26;
                list.Add(k);
                if (k == 0)
                {
                    n = n / 26 - 1;
                }
                else
                {
                    n = n / 26;
                }
            }

            list.Reverse();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                var s = Convert(list[i]);
                sb.Append(s);
            }
            return sb.ToString();
        }
}

https://leetcode.com/problems/excel-sheet-column-title/#/description

 

Java版:

 1 class Solution {
 2     public String convertToTitle(int n) {
 3         StringBuilder stringBuilder = new StringBuilder();
 4         while (n != 0) {
 5             n--; // 依次获取 26 进制逻辑上的个位、十位、百位…(虽然在 26 进制中不这么叫)
 6             stringBuilder.append((char) ('A' + n % 26));
 7             n /= 26;
 8         }
 9         return stringBuilder.reverse().toString();
10     }
11 }

 

posted on 2017-04-26 19:51  Sempron2800+  阅读(107)  评论(0编辑  收藏  举报