【LeetCode】Convert a Number to Hexadecimal 解题报告
2017-01-14 14:39
447 查看
[LeetCode]
https://leetcode.com/problems/convert-a-number-to-hexadecimal/
Difficulty: Easy
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.
对负数的处理不好办。刚开始想用Integer.MAX_VALUE减去负数得到整数,再转成16进制,但是,尝试了之后,正数的最大值得符号位是0,因此这个思路不同。
然后就是想到位运算。>>>的作用是无符号右移。每次右移4位就是相当于除以16,然后再把这个结果对16求余,即可。无论正负都可。因为这就是正确的每四位数划分求对应16进制数的方式。
这里有个技巧,就是hexs[(16 + num % 16) % 16],这样做的目的就是使正负数都能统一计算16进制,不会导致数组溢出。
AC: 8 ms
https://leetcode.com/problems/convert-a-number-to-hexadecimal/
Difficulty: Easy
Question
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
Input: 26 Output: "1a" Input: -1 Output: "ffffffff"
Ways
只管想法就是把数字/16,然后从数组中取字符组成字符串。但这个只对正数有用,负数没法用。对负数的处理不好办。刚开始想用Integer.MAX_VALUE减去负数得到整数,再转成16进制,但是,尝试了之后,正数的最大值得符号位是0,因此这个思路不同。
然后就是想到位运算。>>>的作用是无符号右移。每次右移4位就是相当于除以16,然后再把这个结果对16求余,即可。无论正负都可。因为这就是正确的每四位数划分求对应16进制数的方式。
这里有个技巧,就是hexs[(16 + num % 16) % 16],这样做的目的就是使正负数都能统一计算16进制,不会导致数组溢出。
public class Solution { public String toHex(int num) { char[] hexs = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; StringBuilder answer = new StringBuilder(); if (num == 0) { return "0"; } while (num != 0) { answer.insert(0, hexs[(16 + num % 16) % 16]); num = num >>> 4; } return answer.toString(); } }
AC: 8 ms
Date
2017 年 1 月 14 日相关文章推荐
- LeetCode405 Convert a Number to Hexadecimal
- Leetcode 452. Minimum Number of Arrows to Burst Balloons 射箭 解题报告
- [Leetcode] 452. Minimum Number of Arrows to Burst Balloons 解题报告
- LeetCode -- Convert a Number to Hexadecimal
- [LeetCode]Convert Sorted Array to Binary Search Tree, 解题报告
- 【LeetCode】Convert Sorted List to Binary Search Tree 解题报告
- [LeetCode] 452. Minimum Number of Arrows to Burst Balloons 解题报告
- leetcode[Convert a Number to Hexadecimal]//待整理多种解法
- [LeetCode]Convert a Number to Hexadecimal(Java)
- [Leetcode] Convert a Number to Hexadecimal
- LeetCode405 Convert a Number to Hexadecimal
- leetcode-456-Convert a Number to Hexadecimal
- 【LeetCode】Convert Sorted Array to Binary Search Tree 解题报告
- 【LeetCode】405 Convert a Number to Hexadecimal (java实现)
- 【LeetCode】405 Convert a Number to Hexadecimal (java实现)
- Leetcode 405 Convert a Number to Hexadecimal
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
- LeetCode 405 Convert a Number to Hexadecimal
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
- LeetCode[405] Convert a Number to Hexadecimal