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

Commit a1f4780

Browse files
author
openset
committed
Add: Excel Sheet Column Title
1 parent 15fa572 commit a1f4780

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
package excel_sheet_column_title
1+
package problem_168
22

3+
// Solution 1: recursive
34
func convertToTitle(n int) string {
45
if n--; n < 26 {
56
return string(n + 'A')
67
}
78
return convertToTitle(n/26) + string(n%26+'A')
89
}
10+
11+
// Solution 2: iteration
12+
func convertToTitle2(n int) string {
13+
ans := ""
14+
for ; n > 0; n /= 26 {
15+
n--
16+
ans = string(n%26+'A') + ans
17+
}
18+
return ans
19+
}

‎problems/excel-sheet-column-title/excel_sheet_column_title_test.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package excel_sheet_column_title
1+
package problem_168
22

33
import "testing"
44

@@ -46,10 +46,18 @@ func TestConvertToTitle(t *testing.T) {
4646
expected: "XYZ",
4747
},
4848
}
49+
// Solution 1
4950
for _, tc := range tests {
5051
output := convertToTitle(tc.input)
5152
if output != tc.expected {
5253
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
5354
}
5455
}
56+
// Solution 2
57+
for _, tc := range tests {
58+
output := convertToTitle2(tc.input)
59+
if output != tc.expected {
60+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
61+
}
62+
}
5563
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /