Algorithm: calculate the column number according to the number of excel columns

Posted by Kyrst on Wed, 27 Nov 2019 21:30:56 +0100

I met an algorithm problem when I was in Microsoft's 5 aspects. I calculated the number of columns according to excel column number and calculated the number of columns according to the column number. Because I didn't answer well during the interview, I recorded the implementation idea here.

First of all, let's talk about the following topics: calculate the number of columns according to the excel column number and the number of columns according to the excel column number. The number of columns in excel is represented by letters, that is, A,B,C...Z,AA...,ZZ.... in this case, if we input 1, then the output result is 1, input 27, the output result is AA, and so on.

Train of thought:

In essence, this topic can be simplified as a topic of conversion from 26 to 10, so the idea is very easy:

    /**
     * In essence, it's an algorithm of conversion from 26 base to 10 base, so you can directly take a value from a single digit to the highest digit, and then convert it to the corresponding letter
     * Note: after taking the remaining 0, it just meets 26, others are normal.
     * Algorithmic idea: operation process of circulation
     * Step1.[Take the remainder] take the remainder 26 with the specified natural number n, and get a remainder m. If m = 0, set m ← 26.
     * Step2.[Convert to character] map m to character c, and the mapping rule is {1-26} - > {A-Z}. Then C is spliced to the left of the 26 base value s, that is, set s ← c + s.
     * Step3.[Decaying power] set n ← (n – m)/26. If n > 0, return to step 1 to continue, otherwise enter step 4.
     * Step4.[End] return to s.
     * @param num
     * @return
     */
    public static String numCovertLetter(int num) {
        if (num <= 0) {
            throw new RuntimeException("Parameter must be greater than 0");
        }
        String str = "";
        while (num > 0) {
            int res = num % 26;
            if (res == 0) {
                res = 26;
            }
            str = (char) (res + 64)+ str;
            num = (num - res) / 26;
        }
        return str;
    }

Then, think about transposition. If you need to find out the algorithm of converting other base to 10 base, we can extract 26 and change it to the way of transmission:

   /**
     * Derive the way to convert other base numbers to base numbers
     * @param num
     * @param pos
     * @return
     */
    public static String numCovertLetter(int num,int pos) {
        if (num <= 0) {
            throw new RuntimeException("Parameter must be greater than 0");
        }
        String str = "";
        while (num > 0) {
            int residue = num % pos;
            if (residue == 0) {
                residue = pos;
            }
            str = residue+ str;
            num = (num - residue) / pos;
        }
        return str;
    }

Ok, we are working on a way to calculate the number of columns according to the column number:


   /**
     *
     * @param string
     * @return
     */
    public static int covertLetterToNum(String string) {
        char[] chars=string.toCharArray();
        int value=0;
        int pos=1;
        for (int i = chars.length - 1; i >= 0; i--) {
            int tmp = chars[i]-64;
            value+=(tmp*pos);
            pos*=26;
        }
        return value;
    }

Topics: Mobile Excel