Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc434ea9

Browse files
EASY/src/easy/ExcelSheetColumnTitle.java
1 parent662b166 commitc434ea9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
packageeasy;
2+
/**168. Excel Sheet Column Title QuestionEditorial Solution My Submissions
3+
Total Accepted: 70066
4+
Total Submissions: 306198
5+
Difficulty: Easy
6+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
7+
8+
For example:
9+
10+
1 -> A
11+
2 -> B
12+
3 -> C
13+
...
14+
26 -> Z
15+
27 -> AA
16+
28 -> AB */
17+
publicclassExcelSheetColumnTitle {
18+
publicStringconvertToTitle_accepted(intn) {
19+
/**'Z' is the corner case, so we'll have to special case handling specially, also, we'll have to do (n-1)/26,
20+
* only when this is not equal to 1, we'll continue.*/
21+
StringBuildersb =newStringBuilder();
22+
while(n !=0){
23+
inttemp =n%26;
24+
if(temp ==0)sb.append("Z");
25+
elsesb.append((char)(temp+64));
26+
n = (n-1)/26;
27+
}
28+
returnsb.reverse().toString();
29+
}
30+
31+
publicStringconvertToTitle_accepted_more_beautiful(intn) {
32+
33+
StringBuildersb =newStringBuilder();
34+
while(n !=0){
35+
inttemp = (n-1)%26;
36+
sb.append((char)(temp+65));
37+
n = (n-1)/26;
38+
}
39+
returnsb.reverse().toString();
40+
41+
}
42+
43+
publicstaticvoidmain(String...strings){
44+
ExcelSheetColumnTitletest =newExcelSheetColumnTitle();
45+
// int n = 28899;
46+
// int n = 1;
47+
// int n = 1000000001;
48+
// int n = 26;
49+
// int n = 27;
50+
intn =28;
51+
// int n = 52;
52+
// int n = 53;
53+
// System.out.println((int) 'A');
54+
// System.out.println(1000000001/26);
55+
// System.out.println(25*26);
56+
// System.out.println(26*26);
57+
// System.out.println(27*26);
58+
// System.out.println(702%26);
59+
// System.out.println(702/26);
60+
System.out.println(Integer.parseInt(String.valueOf(26),10));
61+
System.out.println(test.convertToTitle_accepted(n));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp