Leetcode - Excel Sheet Column Title

**
Question:

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 

**

My code:

public class Solution {
    public String convertToTitle(int n) {
        int col = (n - 1) % 26;
        String result = new String();
        while (n != 0) {
            result = result + (char)('A' + col);
            n = (n - 1) / 26;
            col = (n - 1) % 26;
        }
        result = this.reverse(result);
        System.out.println(result);
        return result;
    }
    
    private String reverse(String str) {
        String rev = "";
        for (int i = str.length() - 1; i >= 0; i--) 
            rev += str.charAt(i);
        return rev;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        test.convertToTitle(704);
    }
}

My test result:

Paste_Image.png

Conclusion:
這次題目依然不是很過癮。下次直接挑一道m(xù)edium的題目做做。
還是一個類似于遞歸的思想。
第一次 (n-1) / 26 and (n-1) % 26可以得到最左邊的字母。然后得到第二位,第三位,直到循環(huán)結束。
但有三個問題:
1.如何操作character

String a = 'A' + 1;
System.out.println(a);

打印出來的結果是65,而不是B。因為 'A' + 1 默認返回的結果是一個ASCII值65,然后會存入String中,所以打印出來的結果是 65.
所以必須得強制轉(zhuǎn)換下,將65轉(zhuǎn)換為character。
2.如何將character轉(zhuǎn)換為String

String a = 'A';

會報錯。
兩種方法:
a.

String a = Character.toString('A');

b.

String a = "" + 'A';

額不知道為什么加了 ""之后就可以開始添加character了。

最后的結果與我們所需要的結果是相反的,因為我們是從右往左加字母,而實際是從左往右加字母。所以,我們必須要將整個字符串反轉(zhuǎn)下。于是我自己寫了一個反轉(zhuǎn)方法。的確,這么做會比較消耗資源。

然后,我愚蠢的發(fā)現(xiàn)。字母是可以從右往左加的。也是看了其他大神的代碼,發(fā)現(xiàn)調(diào)換下順序后這個問題就沒了。。。
代碼如下:

public class Solution {
    public String convertToTitle(int n) {
        int col = (n - 1) % 26;
        String result = new String();
        while (n != 0) {
            result = (char)('A' + col) + result;
            n = (n - 1) / 26;
            col = (n - 1) % 26;
        }

        System.out.println(result);
        return result;
    }
}

Test result:

Paste_Image.png

速度快了幾十個ms.我想,不僅僅是速度快了,而且空間也節(jié)省了很多。當然,速度還是遠遠比不上C/C++。

**
總結:
1.character操作
2.character如何轉(zhuǎn)換為string
3.一開始考慮了反轉(zhuǎn),后來發(fā)現(xiàn)完全沒必要了。還是腦子太死了。
參考的兩個網(wǎng)址:
https://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string-in-java/8172439#8172439?newreg=465693d76621479795ad4750c13a7d81

http://stackoverflow.com/questions/7479714/legal-java-operations-with-integers-and-char
**

下次選一道m(xù)edium的題目來試試手。
Good luck, Richardo!

My code:

public class Solution {
    public String convertToTitle(int n) {
        int offset = (n - 1) % 26;
        String ret = "";
        while (n > 0) {
            ret = (char) ('A' + offset) + ret;
            n = (n - 1) / 26;
            offset = (n - 1) % 26;
        }
        
        return ret;
    }
}

這道題目竟然沒能做出來??赡苁潜?br> permutation sequence 搞亂了。
那道題目,n--,就可以將其轉(zhuǎn)換為 index
這道題目很明顯不是。。。
可以這么說,那道題目是平均分配的,每一組個數(shù)都相等。
這里不是。每一組個數(shù)會越來越多。

所以我發(fā)現(xiàn),那道題目,一開始求的是最左邊的數(shù),
而這道題目,根據(jù)它的特性,第一個求的是最右邊的字母。

然后找一下規(guī)律就可以做出來。。

Anyway, Good luck, Richardo! -- 09/05/2016

My code:

public class Solution {
    public String convertToTitle(int n) {
        if (n <= 0) {
            return "";
        }
        
        char[] dic = new char[26];
        for (int i = 1; i < 26; i++) {
            dic[i] = (char) ('A' + i - 1);
        }
        dic[0] = 'Z';
        
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            int digit = n % 26;
            sb.append(dic[digit]);
            n--;
            n = n / 26;
        }
        
        return sb.reverse().toString();
    }
}

這道題目其實還是挺復雜的,看你是否看得穿本質(zhì)。
其實就是一個 26進制轉(zhuǎn)換。
28


1 ... 2 (B)

0 ... 1 (A)

=> AB

但其實沒這么簡單,有特殊情況。
當 n = 26 時, 我會得出 AZ,但其實就是Z
然后加一個 n--就能解決問題。
我不能說出確切的原因,應該是相對位置導致的。
題目給出的順序并不是從0開始的。更具體的說,他的每一層都不是從0開始的,所以,每降一層,都得-1轉(zhuǎn)換到 0-based 系統(tǒng)中。

記住就行了。反正就是進制轉(zhuǎn)換 + 0-based index

Anyway, Good luck, Richardo! -- 09/19/2016

My code:

public class Solution {
    public String convertToTitle(int n) {
        if (n <= 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            n--;
            int offset = n % 26;
            char c = (char) ('A' + offset);
            sb.append(c);
            n = n / 26;
        }
        
        return sb.reverse().toString();
    }
}

Anyway, Good luck, Richardo! -- 10/23/2016

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容