亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[Leetcode] Excel Sheet Column Title Number Convers

JowayYoung / 1429人閱讀

摘要:進(jìn)制復(fù)雜度時(shí)間空間思路得到數(shù)字,其實(shí)就是把進(jìn)制的數(shù)轉(zhuǎn)換為進(jìn)制的數(shù)。算法就是基本的進(jìn)制轉(zhuǎn)換方法,從后往前第位的值乘上。不過因?yàn)槭牵皇?,相?dāng)于進(jìn)制的數(shù)都整體減,才能對(duì)應(yīng)上從開始的十進(jìn)制數(shù)。

Excel Sheet Column Number

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
26進(jìn)制 復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

得到數(shù)字,其實(shí)就是把26進(jìn)制的數(shù)轉(zhuǎn)換為10進(jìn)制的數(shù)。算法就是基本的進(jìn)制轉(zhuǎn)換方法,從后往前第n位的值乘上26^(n-1)。這里26進(jìn)制數(shù)是1開始的,即A是1。

代碼
public class Solution {
    public int titleToNumber(String s) {
        int num = 0, pow = 1;
        for(int i = s.length() - 1; i >= 0 ; i--){
            num += (s.charAt(i) - "A" + 1)*pow;
            pow *= 26;
        }
        return num;
    }
}
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
26進(jìn)制 復(fù)雜度

時(shí)間 O(N) 空間 O(1)

思路

把10進(jìn)制的轉(zhuǎn)換成26進(jìn)制,做法是除26取余,一直除到0,最后把余數(shù)逆序一下就行了。不過因?yàn)锳是1,而不是0,相當(dāng)于26進(jìn)制的數(shù)都整體減1,才能對(duì)應(yīng)上從0開始的十進(jìn)制數(shù)。

代碼
public class Solution {
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while(n != 0){
            sb.append((char)("A" + (n - 1) % 26));
            n = (n - 1) / 26;
        }
        return sb.reverse().toString();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64699.html

相關(guān)文章

  • [LeetCode] 171. Excel Sheet Column Number

    Problem Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... Example 1: Input: A Output: 1 Exa...

    tianlai 評(píng)論0 收藏0
  • [LeetCode] Excel Sheet Column Number

    Problem Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Solution ...

    nifhlheimr 評(píng)論0 收藏0
  • [LeetCode] 168. Excel Sheet Column Title

    Problem 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: 1Output: AEx...

    qc1iu 評(píng)論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月上半月匯總(55 題攻略)

    摘要:微信公眾號(hào)記錄截圖記錄截圖目前關(guān)于這塊算法與數(shù)據(jù)結(jié)構(gòu)的安排前。已攻略返回目錄目前已攻略篇文章。會(huì)根據(jù)題解以及留言內(nèi)容,進(jìn)行補(bǔ)充,并添加上提供題解的小伙伴的昵稱和地址。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目錄 不...

    warmcheng 評(píng)論0 收藏0
  • [LintCode] Excel Sheet Column Number

    Problem Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. Example A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 2...

    xialong 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<