Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 767 Bytes

Excel Sheet Column Number.md

File metadata and controls

31 lines (24 loc) · 767 Bytes

Notes

  • Our standard number system has digits 0 to 9, which is a base 10 system.
  • In this problem we have letters A to Z, so we would like to say this is a base 26 system. However, we have no way to represent 0, so the analogy to a "base something" system cannot be made.

Solution

class Solution {
    public int titleToNumber(String s) {
        if (s == null) {
            return -1;
        }
        int num = 0;
        for (int i = 0; i < s.length(); i++) {
            num *= 26; // 26 possible letters
            num += s.charAt(i) - 'A' + 1;
        }
        return num;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links