-
Notifications
You must be signed in to change notification settings - Fork 0
/
171.excel表列序号.java
67 lines (64 loc) · 1.16 KB
/
171.excel表列序号.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @lc app=leetcode.cn id=171 lang=java
*
* [171] Excel表列序号
*
* https://leetcode-cn.com/problems/excel-sheet-column-number/description/
*
* algorithms
* Easy (66.32%)
* Likes: 116
* Dislikes: 0
* Total Accepted: 32.1K
* Total Submissions: 48.1K
* Testcase Example: '"A"'
*
* 给定一个Excel表格中的列名称,返回其相应的列序号。
*
* 例如,
*
* A -> 1
* B -> 2
* C -> 3
* ...
* Z -> 26
* AA -> 27
* AB -> 28
* ...
*
*
* 示例 1:
*
* 输入: "A"
* 输出: 1
*
*
* 示例 2:
*
* 输入: "AB"
* 输出: 28
*
*
* 示例 3:
*
* 输入: "ZY"
* 输出: 701
*
* 致谢:
* 特别感谢 @ts 添加此问题并创建所有测试用例。
*
*/
// @lc code=start
class Solution {
public int titleToNumber(String s) {
int result = 0;
if(s == null || s.isBlank()) return result;
char[] temp = s.toCharArray();
for (int i = 0; i < temp.length; i++) {
int cNumber = (int)temp[i] - 64;
result = result * 26 + cNumber;
}
return result;
}
}
// @lc code=end