[Leetcode] 405. Convert a Number to Hexadecimal 解题报告
2017-09-22 16:50
387 查看
题目:
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method
is used.
Note:
All letters in hexadecimal (
The hexadecimal string must not contain extra leading
zero, it is represented by a single zero character
in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:
Example 2:
思路:
由于16是2的次幂,并且整数在计算机中都是以二进制的形式表示的,所以转化为16进制的方法非常简单:每次获得二进制的最低4位,直接将其转换为16进制的表示即可。在C++中实现需要注意的一点是:由于有符号整数和无符号整数在负数情况下的循环移位有所不同,所以我们在对num进行循环移位前,需要首先将其显式转换为无符号整数,这样可以统一处理正整数和负整数。
这道题目虽然也可以用传统的除余法解决,但是代码就相对复杂多了,而且处理补码的情况比较麻烦。
代码:
class Solution {
public:
string toHex(int num) {
string ret("");
string hex_chars("0123456789abcdef");
while (num) {
ret.push_back(hex_chars[num & 0xF]);
num = (unsigned)num >> 4;
}
reverse(ret.begin(), ret.end());
return ret.empty() ? "0" : ret;
}
};
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method
is used.
Note:
All letters in hexadecimal (
a-f) must be in lowercase.
The hexadecimal string must not contain extra leading
0s. If the number is
zero, it is represented by a single zero character
'0'; otherwise, the first character
in the hexadecimal string will not be the zero character.
The given number is guaranteed to fit within the range of a 32-bit signed integer.
You must not use any method provided by the library which converts/formats the number to hex directly.
Example 1:
Input: 26 Output: "1a"
Example 2:
Input: -1 Output: "ffffffff"
思路:
由于16是2的次幂,并且整数在计算机中都是以二进制的形式表示的,所以转化为16进制的方法非常简单:每次获得二进制的最低4位,直接将其转换为16进制的表示即可。在C++中实现需要注意的一点是:由于有符号整数和无符号整数在负数情况下的循环移位有所不同,所以我们在对num进行循环移位前,需要首先将其显式转换为无符号整数,这样可以统一处理正整数和负整数。
这道题目虽然也可以用传统的除余法解决,但是代码就相对复杂多了,而且处理补码的情况比较麻烦。
代码:
class Solution {
public:
string toHex(int num) {
string ret("");
string hex_chars("0123456789abcdef");
while (num) {
ret.push_back(hex_chars[num & 0xF]);
num = (unsigned)num >> 4;
}
reverse(ret.begin(), ret.end());
return ret.empty() ? "0" : ret;
}
};
相关文章推荐
- [Leetcode] 311. Sparse Matrix Mult 4000 iplication 解题报告
- [leetcode] 64. Minimum Path Sum 解题报告
- LeetCode解题报告 110. Balanced Binary Tree [easy]
- LeetCode—LRU Cache解题报告
- LeetCode解题报告 257. Binary Tree Paths [easy]
- 【LeetCode】Construct the Rectangle 解题报告
- Leetcode 104. Maximum Depth of Binary Tree 二叉树的最大高度 解题报告
- LeetCode: Valid Palindrome 解题报告
- Leetcode解题报告:406. Queue Reconstruction by Height
- [leetcode] 146. LRU Cache 解题报告
- [Leetcode] 423. Reconstruct Original Digits from English 解题报告
- [leetcode] 57. Insert Interval 解题报告
- LeetCode解题报告--Next Permutation
- [Leetcode] 713. Subarray Product Less Than K 解题报告
- Leetcode 399. Evaluate Division 除法推理 解题报告
- [LeetCode]3Sum,解题报告
- LeetCode(306) Addtive Number解题报告
- LeetCode解题报告--Swap Nodes in Pairs
- [Leetcode] 328. Odd Even Linked List 解题报告
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告