171-excel-sheet-column-number

Question

https://leetcode.com/problems/excel-sheet-column-number/description/

Related to question Excel Sheet Column Title

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

Example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28

Thought Process

  1. Reverse of Mod
    1. Just multiply and add
    2. Time complexity O(n)
    3. Space complexity O(1)

Solution

class Solution {
    public int titleToNumber(String s) {
        char[] chars = s.toCharArray();
        int res = 0;
        for (int i = 0; i < chars.length; i++){
            res = res * 26 + chars[i] - 'A' + 1;
        }
        return res;
    }
}

Additional

results matching ""

    No results matching ""