Skip to content

Commit 9ba3e43

Browse files
author
ChienkuChen
committed
Add 171
1 parent 1d04137 commit 9ba3e43

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package _171;
2+
3+
class Solution {
4+
public int titleToNumber(String s) {
5+
// the ASCII code of A is 65
6+
7+
int sum = 0;
8+
for (int i = 0; i < s.length(); i++) {
9+
sum += Math.pow(26, s.length() - 1 - i) * (s.charAt(i) - 64);
10+
}
11+
12+
return sum;
13+
}
14+
15+
public static void main(String[] args) {
16+
System.out.println(new Solution().titleToNumber("A"));
17+
System.out.println(new Solution().titleToNumber("AB"));
18+
System.out.println(new Solution().titleToNumber("ZY"));
19+
System.out.println(new Solution().titleToNumber("AZ"));
20+
}
21+
}

0 commit comments

Comments
 (0)