zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    sqlplus中文问号
    mysql8.0 Authentication plugin 'caching_sha2_password' cannot be loaded
    Idea2018激活
    bzoj-5049-线段树
    HDU-6070-二分+线段树
    Aizu-2200-floyd+dp
    bzoj-4565-区间dp+状压
    bzoj-3195-状压dp
    bzoj-4870-组合dp+矩阵幂
    swiper使用心得
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8977759.html
Copyright © 2011-2022 走看看