168. Excel Sheet Column Title 由数字返回excel的标题

[抄题]:

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 
    ...

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么分离位数,以为有的取整 有的取余:都是取余来取出一位,然后取整缩小

[一句话思路]:

char ((n - 1) % 26 + 'A') 注意要1

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 注意下新添加的数字若在左边,则需要写成res = new + res

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

取余来取出一位,然后取整缩小

[复杂度]:Time complexity: O(1) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

ans = (char) ((n - 1) % 26 + 'A') + ans;

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {
    public String convertToTitle(int n) {
        String ans = "";
        while (n != 0) {
            ans = (char) ((n - 1) % 26 + 'A') + ans;
            n = (n - 1) / 26;
        }
        return ans;
    }
}
View Code

 

posted @ 2018-05-01 22:05  苗妙苗  阅读(227)  评论(0)    收藏  举报