-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
171.cpp
39 lines (34 loc) · 964 Bytes
/
171.cpp
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
__________________________________________________________________________________________________
sample 4 ms submission
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
class Solution {
public:
// (26 ^ i) * val
int titleToNumber(string s) {
if (s.empty())
return 0;
int num = 0;
int i = s.size() - 1;
for (auto c : s) {
num += pow(26, i) * (c - 'A' + 1);
--i;
}
return num;
}
};
__________________________________________________________________________________________________
sample 7892 kb submission
class Solution {
public:
int titleToNumber(string s) {
int result = 0;
for(int i = 0; i < s.length(); i++){
result = result*26 + (s.at(i) - 'A'+1);
}
return result;
}
};
__________________________________________________________________________________________________