Excel Sheet Column Number

LeetCode Q 171 - Excel Sheet Column Number

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

Example 1: Input: "A" ; Output: 1

Example 2: Input: "AB" ; Output: 28

Example 3: Input: "ZY" ; Output: 701

Solution

Algorithm
Bases Convert

Code:

public int titleToNumber(String s) {
  int res = 0;
  for (int i = 0; i < s.length(); i++) {
    res = res * 26 + (s.charAt(i) - 'A' + 1);
  }
  return res;
}

   Reprint policy


《Excel Sheet Column Number》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC