Back to Leetcode Go

171. Excel Sheet Column Number

website/content.en/ChapterFour/0100~0199/0171.Excel-Sheet-Column-Number.md

1.7.97958 B
Original Source

171. Excel Sheet Column Number

Problem

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 
    ...

Example 1:

Input: "A"
Output: 1

Example 2:

Input: "AB"
Output: 28

Example 3:

Input: "ZY"
Output: 701

Problem Summary

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

Solution Approach

  • Given the name of a column in Excel, output its corresponding column number.
  • Easy problem. This problem is the reverse of Problem 168. Just convert it back to decimal according to base 26.

Code

go

package leetcode

func titleToNumber(s string) int {
	val, res := 0, 0
	for i := 0; i < len(s); i++ {
		val = int(s[i] - 'A' + 1)
		res = res*26 + val
	}
	return res
}