File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode id=171 lang=java
3+ *
4+ * [171] Excel Sheet Column Number
5+ *
6+ * https://leetcode.com/problems/excel-sheet-column-number/description/
7+ *
8+ * algorithms
9+ * Easy (52.16%)
10+ * Likes: 612
11+ * Dislikes: 117
12+ * Total Accepted: 236K
13+ * Total Submissions: 452K
14+ * Testcase Example: '"A"'
15+ *
16+ * Given a column title as appear in an Excel sheet, return its corresponding
17+ * column number.
18+ *
19+ * For example:
20+ *
21+ *
22+ * A -> 1
23+ * B -> 2
24+ * C -> 3
25+ * ...
26+ * Z -> 26
27+ * AA -> 27
28+ * AB -> 28
29+ * ...
30+ *
31+ *
32+ * Example 1:
33+ *
34+ *
35+ * Input: "A"
36+ * Output: 1
37+ *
38+ *
39+ * Example 2:
40+ *
41+ *
42+ * Input: "AB"
43+ * Output: 28
44+ *
45+ *
46+ * Example 3:
47+ *
48+ *
49+ * Input: "ZY"
50+ * Output: 701
51+ *
52+ */
53+ class Solution {
54+ public int titleToNumber (String s ) {
55+ int factor = 1 ;
56+ int number = 0 ;
57+ for (int i = s .length () - 1 ; i >= 0 ; i --) {
58+ number += factor * (s .charAt (i ) - 'A' + 1 );
59+ factor *= 26 ;
60+ }
61+ return number ;
62+ }
63+ }
64+
You can’t perform that action at this time.
0 commit comments