forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0171ExcelSheetColumnNumber.java
More file actions
26 lines (26 loc) · 1.02 KB
/
Copy pathP0171ExcelSheetColumnNumber.java
File metadata and controls
26 lines (26 loc) · 1.02 KB
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
public class P0171ExcelSheetColumnNumber {
public int titleToNumber(String columnTitle){
int res=0;
int multi=1;
for(int i=columnTitle.length()-1;i>=0;i--){ //从后往前,看末位一共进了几次26;首次是26的0次方也就是1;
res=res+(columnTitle.charAt(i)-'A'+1)*multi;
multi*=26;
}
return res;
}
public int titleToNumber0(String columnTitle){
int res=0;
for (int i = 0; i < columnTitle.length(); ++i) {
res *= 26;//相当于向左边位移,类似于十进制中的左移进位
res += (columnTitle.charAt(i)-'A'+1);
}
return res;
}//从前往后,先进位(开始是0)再把末位加进去
}
/*
P0171ExcelSheetColumnNumber p171=new P0171ExcelSheetColumnNumber();
System.out.println(p171.titleToNumber("A"));
System.out.println(p171.titleToNumber("AB"));
System.out.println(p171.titleToNumber("ZY"));
System.out.println(p171.titleToNumber("FXSHRXW"));
*/