zoukankan      html  css  js  c++  java
  • LeetCode 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 

    题目标签:Math

      题目给了我们一个 int n, 让我们返回对应的 excel 表格 纵列的 名称。

      如果是10进制 的数字,我们是用 % 10 来拿到最右边的digit, 用 / 10 来去除最右边的digit,继续下一轮;

      这里也是一样的原理,换成 26进制, 1 - 26 对应 A - Z,唯一要注意的是,这里的A 是从 1 开始的,实质上是应该从 0 开始,所以我们要在每一轮 -1 来取得平衡。

    Java Solution:

    Runtime beats 11.27% 

    完成日期:06/12/2017

    关键词:26进制

    关键点:A 应该从0 开始

     1 class Solution 
     2 {
     3     public String convertToTitle(int n) 
     4     {
     5         String res = "";
     6         
     7         while(n > 0)
     8         {
     9             n--; // for each round, need to -1
    10 
    11             int rml = n % 26; // get right most letter 1 ~ 26 = A ~ Z
    12             res = (char)('A' + rml) + res;
    13             
    14             n = n / 26; // means get rid of right most letter;
    15         }
    16         
    17         return res;
    18     }
    19 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    HTTP——Web服务器、代理、缓存
    nginx配置文件详解2
    nginx配置文件详解
    shell笔记2
    django笔记
    python 发请求,urllib,urllib2
    nginx配置
    python os模块学习
    mac 终端命令小结
    mac常用命令笔记
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/8036010.html
Copyright © 2011-2022 走看看