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

Commit598de6d

Browse files
refactor 168
1 parent46a9e27 commit598de6d

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed
Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,48 @@
11
packagecom.fishercoder.solutions;
2-
/**168. Excel Sheet Column Title
2+
/**
33
*
4-
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
4+
* 168. Excel Sheet Column Title
55
6-
For example:
6+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
77
8-
1 -> A
9-
2 -> B
10-
3 -> C
11-
...
12-
26 -> Z
13-
27 -> AA
14-
28 -> AB */
15-
publicclass_168 {
8+
For example:
169
17-
publicStringconvertToTitle_accepted_more_beautiful(intn) {
18-
/**Get the right most digit first, move to the left, e.g. when n = 28, we get 'B' first, then we get 'A'.*/
19-
StringBuildersb =newStringBuilder();
20-
while (n !=0) {
21-
inttemp = (n -1) %26;
22-
sb.append((char) (temp +65));
23-
n = (n -1) /26;
24-
}
25-
returnsb.reverse().toString();
10+
1 -> A
11+
2 -> B
12+
3 -> C
13+
...
14+
26 -> Z
15+
27 -> AA
16+
28 -> AB
17+
...
2618
27-
}
19+
Example 1:
2820
29-
publicstaticvoidmain(String...strings) {
30-
_168test =new_168();
31-
// int n = 28899;
32-
// int n = 1;
33-
// int n = 1000000001;
34-
// int n = 26;
35-
// int n = 27;
36-
intn =28;
37-
// int n = 52;
38-
// int n = 53;
39-
// System.out.println((int) 'A');
40-
// System.out.println(1000000001/26);
41-
// System.out.println(25*26);
42-
// System.out.println(26*26);
43-
// System.out.println(27*26);
44-
// System.out.println(702%26);
45-
// System.out.println(702/26);
46-
System.out.println(Integer.parseInt(String.valueOf(26),10));
47-
System.out.println(test.convertToTitle_accepted_more_beautiful(n));
48-
}
21+
Input: 1
22+
Output: "A"
4923
50-
publicStringconvertToTitle_accepted(intn) {
51-
/**'Z' is the corner case, so we'll have to special case handling specially, also, we'll have to do (n-1)/26,
52-
* only when this is not equal to 1, we'll continue.*/
53-
StringBuildersb =newStringBuilder();
54-
while (n !=0) {
55-
inttemp =n %26;
56-
if (temp ==0) {
57-
sb.append("Z");
58-
}else {
59-
sb.append((char) (temp +64));
60-
}
61-
n = (n -1) /26;
62-
}
63-
returnsb.reverse().toString();
64-
}
24+
Example 2:
25+
26+
Input: 28
27+
Output: "AB"
6528
66-
}
29+
Example 3:
30+
31+
Input: 701
32+
Output: "ZY"
33+
34+
*/
35+
publicclass_168 {
36+
publicstaticclassSolution1 {
37+
publicStringconvertToTitle(intn) {
38+
/**Get the right most digit first, move to the left, e.g. when n = 28, we get 'B' first, then we get 'A'.*/
39+
StringBuildersb =newStringBuilder();
40+
while (n !=0) {
41+
inttemp = (n -1) %26;
42+
sb.append((char) (temp +65));
43+
n = (n -1) /26;
44+
}
45+
returnsb.reverse().toString();
46+
}
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._168;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_168Test {
10+
privatestatic_168.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_168.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals("APSM",solution1.convertToTitle(28899));
20+
}
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp