Skip to content

Commit 5ca7673

Browse files
authored
Update hexspeak.cpp
1 parent f4f5820 commit 5ca7673

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

C++/hexspeak.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ class Solution {
2222
return result;
2323
}
2424
};
25+
26+
27+
// Time: O(n)
28+
// Space: O(n)
29+
class Solution2 {
30+
public:
31+
string toHexspeak(string num) {
32+
uint64_t n = stoul(num);
33+
stringstream ss;
34+
ss << hex << uppercase << n;
35+
string result(ss.str());
36+
for (auto i = 0; i < result.length(); ++i) {
37+
if ('2' <= result[i] && result[i] <= '9') {
38+
return "ERROR";
39+
}
40+
if (result[i] == '0') {
41+
result[i] = 'O';
42+
}
43+
if (result[i] == '1') {
44+
result[i] = 'I';
45+
}
46+
}
47+
return result;
48+
}
49+
};

0 commit comments

Comments
 (0)