Excel Sheet Column Title
LeetCode 168 | Difficulty: Easyβ
EasyProblem Descriptionβ
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: columnNumber = 1
Output: "A"
Example 2:
Input: columnNumber = 28
Output: "AB"
Example 3:
Input: columnNumber = 701
Output: "ZY"
Constraints:
- `1 <= columnNumber <= 2^31 - 1`
Topics: Math, String
Approachβ
Mathematicalβ
Look for mathematical patterns or formulas. Consider: modular arithmetic, GCD/LCM, prime factorization, combinatorics, or geometric properties.
When to use
Problems with clear mathematical structure, counting, number properties.
String Processingβ
Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.
When to use
Anagram detection, palindrome checking, string transformation, pattern matching.
Solutionsβ
Solution 1: C# (Best: 108 ms)β
| Metric | Value |
|---|---|
| Runtime | 108 ms |
| Memory | N/A |
| Date | 2018-03-30 |
Solution
public class Solution {
public string ConvertToTitle(int n) {
string sb = string.Empty;
while(n>0)
{
n--;
var rem = n%26;
n=n/26;
sb = ((char)('A'+rem))+ sb;
}
return sb;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | $O(n)$ | $O(1) to O(n)$ |
Interview Tipsβ
Key Points
- Start by clarifying edge cases: empty input, single element, all duplicates.