-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_171.java
More file actions
37 lines (33 loc) · 810 Bytes
/
Copy pathjava_171.java
File metadata and controls
37 lines (33 loc) · 810 Bytes
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
package leetcode;
public class java_171 {
/**
* 26½øÖÆ->Ê®½øÖÆ
* Given a column title as appear in an Excel sheet, return its
* corresponding column number.
* For example: A -> 1 B -> 2 C -> 3 ... Z ->26 AA -> 27 AB -> 28
*
* @author rwxn
*/
public int titleToNumber(String s) {
int Mid_count = 0;
int Title_Number = 0;
int len = s.length();
s = s.toUpperCase();
// if(len == 0){
// return 0;
// }
for (int index = len - 1; index >= 0; index--) {
int unit = 1;
for (int i = len - index - 1; i > 0; i--) {
unit *= 26;
}
if (index == len - 1) {
Mid_count = (int) s.charAt(index) - 64;
} else {
Mid_count = ((int) s.charAt(index) - 64) * unit;
}
Title_Number += Mid_count;
}
return Title_Number;
}
}