[LeetCode 168] Excel Sheet Column Title
LeetCode 168 (Java)
[Excel Sheet Column Title] 문제 풀이
[LeetCode 168] Excel Sheet Column Title
Description
Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.
For example:
1
2
3
4
5
6
7
8
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
Code
내 제출
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public String convertToTitle(int columnNumber) {
StringBuilder res = new StringBuilder();
while (columnNumber != 0) {
--columnNumber;
res.append((char) ('A' + columnNumber % 26));
columnNumber /= 26;
}
return res.reverse().toString();
}
}
| Runtime | Memory |
|---|---|
| 0 ms | 40.9 MB |
다른 풀이
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public String convertToTitle(int columnNumber) {
String result="";
while(columnNumber>0){
columnNumber--;
result=(char)((columnNumber%26)+'A')+result;
columnNumber/=26;
}
return result;
}
}
Reference
- https://github.com/doocs/leetcode/blob/main/solution/0100-0199/0168.Excel%20Sheet%20Column%20Title/Solution.java
This post is licensed under CC BY 4.0 by the author.
