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:

《Leetcode - Excel Sheet Column Title》 Paste_Image.png

Conclusion:
这次题目依然不是很过瘾。下次直接挑一道medium的题目做做。
还是一个类似于递归的思想。
第一次 (n-1) / 26 and (n-1) % 26可以得到最左边的字母。然后得到第二位,第三位,直到循环结束。
但有三个问题:
1.如何操作character

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

打印出来的结果是65,而不是B。因为 ‘A’ + 1 默认返回的结果是一个ASCII值65,然后会存入String中,所以打印出来的结果是 65.
所以必须得强制转换下,将65转换为character。
2.如何将character转换为String

String a = 'A';

会报错。
两种方法:
a.

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

b.

String a = "" + 'A';

额不知道为什么加了 “”之后就可以开始添加character了。

最后的结果与我们所需要的结果是相反的,因为我们是从右往左加字母,而实际是从左往右加字母。所以,我们必须要将整个字符串反转下。于是我自己写了一个反转方法。的确,这么做会比较消耗资源。

然后,我愚蠢的发现。字母是可以从右往左加的。也是看了其他大神的代码,发现调换下顺序后这个问题就没了。。。
代码如下:

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:

《Leetcode - Excel Sheet Column Title》 Paste_Image.png

速度快了几十个ms.我想,不仅仅是速度快了,而且空间也节省了很多。当然,速度还是远远比不上C/C++。

**
总结:
1.character操作
2.character如何转换为string
3.一开始考虑了反转,后来发现完全没必要了。还是脑子太死了。
参考的两个网址:
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
**

下次选一道medium的题目来试试手。
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;
    }
}

这道题目竟然没能做出来。可能是被
permutation sequence 搞乱了。
那道题目,n–,就可以将其转换为 index
这道题目很明显不是。。。
可以这么说,那道题目是平均分配的,每一组个数都相等。
这里不是。每一组个数会越来越多。

所以我发现,那道题目,一开始求的是最左边的数,
而这道题目,根据它的特性,第一个求的是最右边的字母。

然后找一下规律就可以做出来。。

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();
    }
}

这道题目其实还是挺复杂的,看你是否看得穿本质。
其实就是一个 26进制转换。
28

1 … 2 (B)

0 … 1 (A)

=> AB

但其实没这么简单,有特殊情况。
当 n = 26 时, 我会得出 AZ,但其实就是Z
然后加一个 n–就能解决问题。
我不能说出确切的原因,应该是相对位置导致的。
题目给出的顺序并不是从0开始的。更具体的说,他的每一层都不是从0开始的,所以,每降一层,都得-1转换到 0-based 系统中。

记住就行了。反正就是进制转换 + 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

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/de01161f3daa#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞