LeetCode 171. Excel Sheet Column Number
原题链接在这里:https://leetcode.com/problems/excel-sheet-column-number/
题目:
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
题解:
Keep accumlating.
Time O(n). n = s.length().
Space O(1).
AC Java:
1 public class Solution { 2 public int titleToNumber(String s) { 3 if(s == null || s.length() == 0){ 4 throw new IllegalArgumentException("Invalid input string."); 5 } 6 int res = 0; 7 for(int i = 0; i<s.length(); i++){ 8 res = res*26 + (int)(s.charAt(i)-'A') + 1; 9 } 10 return res; 11 } 12 }