Excel Sheet Column Number, sheetcolumn

Source: Internet
Author: User

Excel Sheet Column Number, sheetcolumn

This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/42290079

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 


Ideas:

(1) The question is to give the character of the corresponding column in Excel to find the corresponding integer of this character.

(2) This simple question examines the question of "Twenty-six hexadecimal" (not actually twenty-six hexadecimal ). It is not difficult to find that when the value exceeds 26, it will change like decimal. I will not explain it here.

(3) Taking into account the simplicity and efficiency, the integer corresponding to the 26 characters is directly stored in a Map. It must be noted that the power () method of the Math class is used in the algorithm. This method is a power function.

For example, for the character "ABC", the solution process is:

Count = 0 + 1 * Math. power (26, 2) = 676;

Count = 676 + 2 * Math. power (26, 1) = 728;

Count = 728 + 3 * Math. power (26, 0) = 731;

(4) I hope this article will help you.


The algorithm code is implemented as follows:

public static int titleToNumber(String s) {Map<String, Integer> all = new HashMap<String, Integer>();all.put("A", 1);all.put("B", 2);all.put("C", 3);all.put("D", 4);all.put("E", 5);all.put("F", 6);all.put("G", 7);all.put("H", 8);all.put("I", 9);all.put("J", 10);all.put("K", 11);all.put("L", 12);all.put("M", 13);all.put("N", 14);all.put("O", 15);all.put("P", 16);all.put("Q", 17);all.put("R", 18);all.put("S", 19);all.put("T", 20);all.put("U", 21);all.put("V", 22);all.put("W", 23);all.put("X", 24);all.put("Y", 25);all.put("Z", 26);if (s == null)return -1;int len = s.length();int sum = 0;int power = len - 1;for (int i = 0; i < len; i++) {int curr = all.get(String.valueOf(s.charAt(i)));sum += curr * Math.pow(all.size(), power);power--;}return sum;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.