【LeetCode】405 Convert a Number to Hexadecimal (java实现)
2019独角兽企业重金招聘Python工程师标准>>>
原题链接
https://leetcode.com/problems/convert-a-number-to-hexadecimal/
原题
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:
26Output:
"1a"
Example 2:
Input:
-1Output:
"ffffffff"
题目要求
题目叫“将数字转化为十六进制”,顾名思义,这里需要注意的是数字是包含负数的,所以如果方法不很合适,处理起来会稍微麻烦一些。
要求:
- 转化后的十六进制字符串都是小写的;
- 十六进制字符串不能以0开头(如果只有一个0除外);
- 数字大小在32bit范围内,不用担心处理数据时溢出;
- 不能使用库里的转化和格式打印;
解法
解法一:最原始的方法,完全按照数字的源码、反码、补码的格式来转化,这种思路下,就要先将数字转化为2进制,再将二进制转化为十六进制。同时,还需要注意数字为负数时,需要一些特殊的操作。这种解法非常麻烦,但是却非常直接。
public String toHex(int num) {if (num == 0) {return "0";}int MAX = 32;boolean isNegative = false;int bits[] = new int[MAX];if (num < 0) {isNegative = true;bits[MAX - 1] = 1;num = -num;}int i = 0;// 转化为二进制的原码while (num > 0) {bits[i++] = num % 2;num /= 2;}// 如果是负数,需要取反并且+1从而得到补码if (isNegative) {// 取反for (int j = 0; j < bits.length - 1; j++) {bits[j] = (bits[j] + 1) % 2;}// +1int digit = 1;int res = 0;for (int j = 0; j < bits.length - 1; j++) {res = bits[j] + digit;bits[j] = res % 2;digit = res / 2;}}// 二进制转化为十六进制String ret = "";for (int j = 0; j < bits.length; j += 4) {int data = 0;for (int j2 = 0; j2 < 4; j2++) {data += bits[j + j2] * (1 << j2);}ret = String.format("%x", data) + ret;}// 去掉字符串前面多余的0for (int j = 0; j < ret.length(); j++) {if (ret.charAt(j) != '0') {ret = ret.substring(j);break;}}return ret;
}
解法二:第二种解法就是按位与来获取。既然是得到十六进制,那么每次与上0xF(二进制就是1111),得到一个值,然后数字向右移动4位,这里需要注意的是数字是有符号的,刚好可以利用Java提供的无符号移动>>>。完美!!!
char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
public String toHex(int num) {if(num == 0) return "0";String result = "";while(num != 0){result = map[(num & 0xF)] + result; num = (num >>> 4);}return result;
}
转载于:https://my.oschina.net/styshoo/blog/780869
【LeetCode】405 Convert a Number to Hexadecimal (java实现)相关推荐
- LeetCode算法题-Convert a Number to Hexadecimal(Java实现)
这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...
- LeetCode (12.整数转罗马数字)JAVA StringBuffer
LeetCode (12.整数转罗马数字)JAVA StringBuffer 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 1 ...
- LeetCode(13.罗马数字转整数) JAVA Hashmap
LeetCode(13.罗马数字转整数) JAVA Hashmap 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...
- LeetCode: 109. Convert Sorted List to Binary Search Tree
题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...
- Failed to convert property value of type java.lang.String to required type java.lang.Integer for pro
Failed to convert property value of type java.lang.String to required type java.lang.Integer for pro ...
- leetcode 321 Create Max Number
leetcode 321 Create Max Number greedy的方法,由于有两个数组,我们很自然的想到从数组1中选i个数,数组2中选k-i个数,这样我们只需要遍历max(0, k-数组2长 ...
- 【链表递归构造二叉树】LeetCode 109. Convert Sorted List to Binary Search Tree
LeetCode 109. Convert Sorted List to Binary Search Tree Solution1:我的答案 偷鸡摸狗的做法 /*** Definition for s ...
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- LeetCode 09:回文数(Java实现)
LeetCode 09:回文数(Java实现) 题目 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121 输出: true 示例 2:输 ...
最新文章
- Eclipse下修改工程名
- 【Android 进程保活】提升进程优先级 ( 使用前台 Service 提高应用进程优先级 | 效果展示 | 源码资源 )
- nginx 中location中root和alias的区别
- 【DevOps】为什么我们永远疲于奔命?
- SAP UI5 Opportunity type long description empty issue
- poj2385 基础的动态规划算法 挑战程序设计竞赛
- LeetCode 1111. 有效括号的嵌套深度
- python有多少个模块_python绘图模块有哪些
- 22.Proxy Objects
- nginx安装错误:c compiler cc is not found
- 初中计算机成绩评定方案,初中信息技术学科评价方案
- 服务器鼠标键盘进系统不能用,笔记本开机后鼠标键盘都不能用了怎么办?
- 基于ENVI的Landsat 7地表温度(LST)大气校正方法反演与地物温度分析
- HTTP常见状态码及常见错误
- 第三章:Servlet基础
- MacBook Pro使用记录(一):手动清理内存
- Qt中QList用法详解
- 谐振电路及品质因数(二)
- 【Linux从青铜到王者】第二十篇:Linux网络基础第三篇之IP协议
- ask函数有几个形式参数HTML,第七章 函数
热门文章
- [BUUCTF-pwn]——[BJDCTF 2nd]r2t3
- c++11-noexcept
- linux教程期末考试,Linux-期末考试试题8套含答案.doc
- 5G NR中的两套绝对频域位置:GSCN和ARFCN
- 使用Maven管理Eclipse Java项目(多modules编译)
- 解决Web部署 svg/woff/woff2字体 404错误
- javascript forEach无法break,使用every代替
- Leetcode-53 Maximum Subarray
- LeetCode Combination Sum
- 【转】XMPP_3920_最靠谱的中文翻译文档