zoukankan      html  css  js  c++  java
  • 进制问题

    leetCode上有这么一道题

    Excel Sheet Column Title

    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 

    分析之后可知是一道进制转换的问题,分析的时候偶然间想到digital root的问题,digital root有统一的表达式,是一个9进制问题的转换,如下:

    • dr(n) = 1 + (n - 1) % 9

    而此题呢,也很类似,因为字符从A到Z分别代表1到26时,就是26进制,但是没有0,所以我们也可以将数字减1处理,像是错位一般,一切秩序便井然有序了:

     1 public class Solution {
     2     public String convertToTitle(int n) {
     3         int remainder = 0;
     4         String result = "";
     5         while(n!= 0){
     6             remainder = (n - 1) % 26;
     7             n = (n - 1) / 26;
     8             result = (char)('A' + remainder) + result;
     9         }
    10         return result;
    11     }
    12 }
  • 相关阅读:
    java配置环境变量
    What Beautiful HTML Code Looks Like jessica
    Hive 快速搭建
    Apq本地工具集
    SQL:查询购买了所有指定商品的人
    NodeJs
    留存
    markdown
    微信公众号开发
    viper
  • 原文地址:https://www.cnblogs.com/vin-yuan/p/5365699.html
Copyright © 2011-2022 走看看