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 4eb397b

Browse files
committed
Add 0168 solution.java and README.md
1 parent bd9ce2e commit 4eb397b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Excel表列名称
2+
### 题目描述
3+
4+
给定一个正整数,返回它在 Excel 表中相对应的列名称。
5+
6+
例如,
7+
```
8+
1 -> A
9+
2 -> B
10+
3 -> C
11+
...
12+
26 -> Z
13+
27 -> AA
14+
28 -> AB
15+
...
16+
```
17+
18+
**示例 1:**
19+
```
20+
输入: 1
21+
输出: "A"
22+
```
23+
24+
**示例 2:**
25+
```
26+
输入: 28
27+
输出: "AB"
28+
```
29+
30+
**示例 3:**
31+
```
32+
输入: 701
33+
输出: "ZY"
34+
```
35+
36+
### 解法
37+
1. 将数字 n 减一,则尾数转换为 0-25 的 26 进制数的个位;用除 26 取余的方式,获得尾数对应的符号。
38+
2. 用除26取余的方式,获得尾数对应的符号;
39+
3. 重复步骤1、2直至 n 为 0。
40+
41+
```java
42+
class Solution {
43+
public String convertToTitle(int n) {
44+
if (n < 0) {
45+
return "";
46+
}
47+
StringBuilder sb = new StringBuilder();
48+
while (n > 0) {
49+
n--;
50+
int temp = n % 26;
51+
sb.insert(0,(char)(temp + 'A'));
52+
n /= 26;
53+
}
54+
return sb.toString();
55+
}
56+
}
57+
58+
```
59+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public String convertToTitle(int n) {
3+
if (n < 0) {
4+
return "";
5+
}
6+
StringBuilder sb = new StringBuilder();
7+
while (n > 0) {
8+
n--;
9+
int temp = n % 26;
10+
sb.insert(0,(char)(temp + 'A'));
11+
n /= 26;
12+
}
13+
return sb.toString();
14+
}
15+
}

0 commit comments

Comments
(0)

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