blob: 81470fd3880562e4c6023aba0bef3e264bae8f4c (
plain)
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
48
49
50
51
52
53
54
55
|
#include "leetcode.h"
#include <string>
/*
* given integer 'num', return string representing hexidecimal value.
* for negative numbers, use two's complement method
*/
class Solution {
public:
string toHex(int num) {
string ans;
while (num) {
int rem = num % 16;
if (rem > 9)
switch (rem) {
case 10:
ans = 'a' + ans;
break;
case 11:
ans = 'b' + ans;
break;
case 12:
ans = 'c' + ans;
break;
case 13:
ans = 'd' + ans;
break;
case 14:
ans = 'e' + ans;
break;
case 15:
ans = 'f' + ans;
break;
}
else
ans = char(rem + 48) + ans;
num /= 16;
}
return ans;
}
int toBinary(int num) {
int ans = 0;
return ans;
}
};
int main() {
Solution obj;
// obj.toHex(26);
cout << obj.toHex(26) << endl; // expect: "1a"
cout << obj.toHex(-1) << endl; // expect: "fffffff"
cout << obj.toBinary(26) << endl; // expect: 11010
cout << obj.toBinary(-1) << endl; // expect:
}
|