p171

LeetCode 171 Excel Sheet Column Number 题解

1.题目:

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 

题意:

26进制

2.解题思路:

26进制

3.代码


[title] [] [url] [link text]
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
 
package leetcode;



public class p171 {
public static void main(String[] args) {
int ans = 0;
String s = "AC";
ans = titleToNumber(s);
System.out.println(ans);

}

public static int titleToNumber(String s) {
int ans = 0;

for (int i = s.length() - 1; i >= 0; i--) {
ans = (int) (ans + (s.charAt(i) - 'A' + 1)
* Math.pow(26, s.length() - 1 - i));
}
return ans;
}

}




4.一些总结: