zoukankan      html  css  js  c++  java
  • 168. Excel Sheet Column Title

    一、题目

      1、审题

      

      

      2、分析

        给出一个正整数,给出如上对应方式对应的字符串。

    二、解答

      1、思路:

        sheet 中,1 —— 26 对应 A——Z;

        而, A——Z 对应的下标为 0——25;

        所以 每次计算时  n--;

        /*
         * I think the reason we do n-- is that number 0 is not included in this sheet. 
         * We can consider this transformation as base-26. However, classic base-26 consists of 
         * number from 0 to 25, and in this case we have numbers from 1 to 26. Now n-- seems intuitive and reasonable.
         */
        public String convertToTitle2(int n) {
         
            StringBuilder sb = new StringBuilder();
            
            while(n > 0) {
                n--;
                sb.append((char)('A' + n % 26)); // 'A' 自带了 1 个单位
                n /= 26;
            }
    
            return sb.reverse().toString();
        }
        
  • 相关阅读:
    Ubuntu
    VSCode
    VSCode
    Astyle
    Qt
    待办
    Qt
    Qt
    Qt
    python pip常用命令、配置pip源
  • 原文地址:https://www.cnblogs.com/skillking/p/9797551.html
Copyright © 2011-2022 走看看