【leetcode】 Excel_Sheet_Column_Title

来源:互联网 发布:精品家具淘宝店 编辑:程序博客网 时间:2024/05/22 15:20

题目 :

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 

解析:常规题,找到规律就行了。

 public String convertToTitle(int n) { if(n<27) return character(n);        int num=0;        String str=""; while(n!=0){        num=n%26;        str=character(num)+str;        System.out.println(character(num));        n=(n-1)/26;        } return str; }  public String character(int num){ switch (num) {case 1:return "A";case 2:return "B";case 3:return "C";case 4:return "D";case 5:return "E";case 6:return "F";case 7:return "G";case 8:return "H";case 9:return "I";case 10:return "J";case 11:return "K";case 12:return "L";case 13:return "M";case 14:return "N";case 15:return "O";case 16:return "P";case 17:return "Q";case 18:return "R";case 19:return "S";case 20:return "T";case 21:return "U";case 22:return "V";case 23:return "W";case 24:return "X";case 25:return "Y";default:return "Z";} }


0 0