Skip to content

171. Excel Sheet Column Number 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  int titleToNumber(string columnTitle) {
    return accumulate(
        columnTitle.begin(), columnTitle.end(), 0,
        [](int subtotal, char c) { return subtotal * 26 + (c - 'A' + 1); });
  }
};
1
2
3
4
5
class Solution {
  public int titleToNumber(String columnTitle) {
    return columnTitle.chars().reduce(0, (subtotal, c) -> subtotal * 26 + c - '@');
  }
}
1
2
3
4
class Solution:
  def titleToNumber(self, columnTitle: str) -> int:
    return functools.reduce(lambda subtotal, c:
                            subtotal * 26 + ord(c) - ord('@'), columnTitle, 0)