Skip to content

Commit 2d5a2fe

Browse files
committed
Create 1271-Hexspeak.cpp
1 parent d04a358 commit 2d5a2fe

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1271-Hexspeak.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string toHexspeak(string num) {
4+
long n(0);
5+
for(int p = 0; p < num.size(); p++){n = 10 * n + (num[p] - '0');}
6+
7+
std::string s;
8+
while(n){
9+
int m = n % 16;
10+
if(1 < m && m < 9){return "ERROR";}
11+
12+
if(m == 0){s += 'O';}
13+
else if(m == 1){s += "I";}
14+
else{s += (char)((m - 10 ) + 'A');}
15+
n /= 16;
16+
}
17+
18+
std::reverse(s.begin(), s.end());
19+
return s;
20+
}
21+
};

0 commit comments

Comments
 (0)