-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathExcelSheetColumnNumber.java
More file actions
55 lines (53 loc) · 1.42 KB
/
ExcelSheetColumnNumber.java
File metadata and controls
55 lines (53 loc) · 1.42 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
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
// Source : https://leetcode.com/problems/excel-sheet-column-number/
// Author : Diego Ruiz Piqueras (Pikeras72)
// Date : 24-04-2022
/*****************************************************************************************************
* Given a string columnTitle that represents the 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
* ...
*
* Example 1:
*
* Input: columnTitle = "A"
* Output: 1
*
* Example 2:
*
* Input: columnTitle = "AB"
* Output: 28
* Explanation:
*
* Example 3:
*
* Input: columnTitle = "ZY"
* Output: 701
*
* 1 <= columnTitle.length <= 7
* columnTitle consists only of uppercase English letters.
* columnTitle is in the range ["A", "FXSHRXW"]
******************************************************************************************************/
class Solution {
public int titleToNumber(String columnTitle) {
int res = 0, cnt = 0;
String letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int exponente = columnTitle.length()-1;
if(exponente == 0){
return letras.indexOf(columnTitle)+1;
}
while(exponente != -1){
res += (int) ((letras.indexOf(String.valueOf(columnTitle.charAt(cnt)))+1)*Math.pow(26, exponente));
exponente--;
cnt++;
}
return res;
}
}