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();
        }
        
  • 相关阅读:
    Mysql索引优化分析
    mysql学习
    linux安装mysql
    MVC实例应用模式
    模型-视图-控制器MVC模式
    设计模式理解
    XX系统质量属性战术
    XX系统可用性易用性
    属性常见属性场景
    架构漫谈读后感
  • 原文地址:https://www.cnblogs.com/skillking/p/9797551.html
Copyright © 2011-2022 走看看