171. Excel Sheet Column Number

mac2026-08-13  4

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

二十六进制 从前往后遍历字符,获得字符代表的值val

result = result * 26 + val class Solution { public int titleToNumber(String s) { int result = 0; char offSet = 'A'; int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); int val = c - offSet + 1; result = (26 * result + val); } return result; } }
最新回复(0)