101 - 数字转换为16进制数

题目

给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。

注意:

  1. 十六进制中所有字母(a-f)都必须是小写。

  2. 十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。

  3. 给定的数确保在32位有符号整数范围内。

  4. 不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。

示例 1:

输入: 26

输出: "1a"

示例 2:

输入: -1

输出: "ffffffff"

解答

&

https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/89261/easy-10-line-python-solution-with-inline-explanation

Runtime: 36 ms, faster than 70.37% of Python3 online submissions for Convert a Number to Hexadecimal.

Memory Usage: 13.8 MB, less than 50.00% of Python3 online submissions for Convert a Number to Hexadecimal.

Runtime: 44 ms, faster than 98.65% of JavaScript online submissions for Convert a Number to Hexadecimal.

Memory Usage: 33.8 MB, less than 100.00% of JavaScript online submissions for Convert a Number to Hexadecimal.

Runtime: 0 ms, faster than 100.00% of Go online submissions for Convert a Number to Hexadecimal.

Memory Usage: 1.9 MB, less than 100.00% of Go online submissions for Convert a Number to Hexadecimal.

%

作者:trklnejQEZ 链接:https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/python-chao-jian-dan-shi-xian-bao-ni-ke-yi-kan-don/

Runtime: 32 ms, faster than 90.74% of Python3 online submissions for Convert a Number to Hexadecimal.

Memory Usage: 13.8 MB, less than 50.00% of Python3 online submissions for Convert a Number to Hexadecimal.

Runtime: 0 ms, faster than 100.00% of Go online submissions for Convert a Number to Hexadecimal.

Memory Usage: 2 MB, less than 100.00% of Go online submissions for Convert a Number to Hexadecimal.

Runtime: 44 ms, faster than 98.65% of JavaScript online submissions for Convert a Number to Hexadecimal.

Memory Usage: 33.9 MB, less than 100.00% of JavaScript online submissions for Convert a Number to Hexadecimal.

Last updated

Was this helpful?