Skip to content

Add 0168 solution.java and README.md #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add 0168 solution.java and README.md
  • Loading branch information
Mrzhudky committed Mar 30, 2019
commit 4eb397b9e132dadb4c2ac9fe1b9ff4d284e5de6b
59 changes: 59 additions & 0 deletions solution/0168.Excel Sheet Column Title/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Excel表列名称
### 题目描述

给定一个正整数,返回它在 Excel 表中相对应的列名称。

例如,
```
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
```

**示例 1:**
```
输入: 1
输出: "A"
```

**示例 2:**
```
输入: 28
输出: "AB"
```

**示例 3:**
```
输入: 701
输出: "ZY"
```

### 解法
1. 将数字 n 减一,则尾数转换为 0-25 的 26 进制数的个位;用除 26 取余的方式,获得尾数对应的符号。
2. 用除26取余的方式,获得尾数对应的符号;
3. 重复步骤1、2直至 n 为 0。

```java
class Solution {
public String convertToTitle(int n) {
if (n < 0) {
return "";
}
StringBuilder sb = new StringBuilder();
while (n > 0) {
n--;
int temp = n % 26;
sb.insert(0,(char)(temp + 'A'));
n /= 26;
}
return sb.toString();
}
}

```

15 changes: 15 additions & 0 deletions solution/0168.Excel Sheet Column Title/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public String convertToTitle(int n) {
if (n < 0) {
return "";
}
StringBuilder sb = new StringBuilder();
while (n > 0) {
n--;
int temp = n % 26;
sb.insert(0,(char)(temp + 'A'));
n /= 26;
}
return sb.toString();
}
}