405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
注意:
十六进制中所有字母(a-f)都必须是小写。
十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。
给定的数确保在32位有符号整数范围内。
不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。
示例 1:
输入:
26
输出:
"1a"
示例 2:
输入:
-1
输出:
"ffffffff"
详见:https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/
C++:
class Solution {
public:
string toHex(int num) {
string hexString = "";
string hexChar = "0123456789abcdef";
while (num)
{
hexString = hexChar[num & 0xF] + hexString;
num = (unsigned)num >> 4;
}
return hexString.empty() ? "0" : hexString;
}
};
参考:https://www.cnblogs.com/grandyang/p/5926674.html
405 Convert a Number to Hexadecimal 数字转换为十六进制数的更多相关文章
- Leetcode405Convert a Number to Hexadecimal数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果要转化的数 ...
- 38. leetcode 405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecim ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/ 分析:10进制转换成16进制,不能用库函数,刚开始,我 ...
- 405. Convert a Number to Hexadecimal
..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...
- LeetCode_405. Convert a Number to Hexadecimal
405. Convert a Number to Hexadecimal Easy Given an integer, write an algorithm to convert it to hexa ...
- Java实现 LeetCode 405 数字转换为十六进制数
405. 数字转换为十六进制数 给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能 ...
随机推荐
- codevs 3498 小木棍
3498 小木棍 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 乔治有一些同样长的小木棍,他把这些木棍随意 ...
- JSP的表单处理
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/form-processing.html: 当需要从浏览器向Web服务器传递一些信息并最终将信息返回到后端 ...
- php-7.1编译记录
编译php-7.1.28步骤 检查环境 ./configure \ --prefix=/u01/server/php-7.1.28 \ --enable-fpm \ --with-fpm-user=d ...
- SVG :可缩放矢量图形(Scalable Vector Graphics)。
SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失 SVG 使用 XML ...
- 多线程TcpServer
多线程TcpServer自己的EventLoop只用来接收新连接(即TcpServer所属线程的EventLoop只监听listen fd),而新连接会用其他EventLoop来执行IO(即每个新Tc ...
- 浅谈cookie,sessionStorage和localStorage区别
在客户端存储数据可以使用的技术有如下四种: Cookie技术:浏览器兼容性好,但操作比较复杂,需要程序员自己封装,源生的Cookie接口不友好 H5 WebStorage:不能超过8MB,操作简单: ...
- Go语言核心之美 3.2-slice切片
Slice(切片)是长度可变的元素序列(与之相应,上一节中的数组是不可变的),每一个元素都有同样的类型.slice类型写作[]T.T是元素类型.slice和数组写法非常像,差别在于slice没有指定长 ...
- VirtualMachineManager
Java Code Examples for com.sun.jdi.VirtualMachineManager https://www.programcreek.com/java-api-examp ...
- Array.Copy vs Buffer.BlockCopy
http://stackoverflow.com/questions/1389821/array-copy-vs-buffer-blockcopy Since the parameters to Bu ...
- 【ZJOI2009】【Codevs 2347】假期的宿舍
http://codevs.cn/problem/2347/ Solution 二分图板子 连边:i认识j并且j是在校有床 i→j+n i有床i→i+n 还有就是找要在学校的人,1.有床不回2.没床的 ...