forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexspeak.cpp
47 lines (45 loc) · 1.12 KB
/
hexspeak.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
40
41
42
43
44
45
46
47
// Time: O(n)
// Space: O(1)
class Solution {
public:
string toHexspeak(string num) {
unordered_map<int, char> lookup = {{0, 'O'}, {1,'I'}};
for (int i = 0; i < 6; ++i) {
lookup[10 + i] = 'A' + i;
}
string result;
uint64_t n = stoul(num), r = 0;
while (n) {
r = n % 16;
n /= 16;
if (!lookup.count(r)) {
return "ERROR";
}
result.push_back(lookup[r]);
}
reverse(result.begin(), result.end());
return result;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
string toHexspeak(string num) {
stringstream ss;
ss << hex << uppercase << stoul(num);
string result(ss.str());
for (auto i = 0; i < result.length(); ++i) {
if ('2' <= result[i] && result[i] <= '9') {
return "ERROR";
}
if (result[i] == '0') {
result[i] = 'O';
}
if (result[i] == '1') {
result[i] = 'I';
}
}
return result;
}
};