[leetcode] 405. Convert a Number to Hexadecimal
https://leetcode.com/contest/6/problems/convert-a-number-to-hexadecimal/
分析:10进制转换成16进制,不能用库函数,刚开始,我被误导,一直到考虑负数怎么表示成补码,其实,这个系统已经帮我们做好了,计算机里面就是二进制补码表示的,我们需要做的,就是把数字表示成32位二进制,然后每四位映射成一个16进制数字,最后去掉前导0,然后就ok。
注意:不要被什么正数,负数,0,如何表示成二进制补码吸引注意力。
string toHex(int num) {
string res = "";
for (int i = ; i < ; i++){
res.append(, "0123456789abcdef"[num & ]);
num >>= ;
}
reverse(res.begin(), res.end());
while(res.size() > && res[] == '')
res = res.substr();
return res;
}
string s[] = {"", "","", "",
"", "","", "",
"", "","", "",
"", "","", ""
};
char ch[] = {'','','','',
'','','','',
'','','a','b',
'c','d','e','f'
};
class Solution {
public:
string toHex(int num) {
if(num == ) {
return "";
}
if(num == ) {
return "";
}
if(num == INT_MIN) {
return "";
}
string t = "";
for (int i = ; i >= ; i--) {
if(num & ( << i)) t.append(, '');
else t.append(, '');
}
map<string , char> m;
for (int i = ; i < ; i++) {
m[s[i] ] = ch[i];
}
string res = "";
for (int i = ; i < ; i += ) {
string td = t.substr(i, );
res.append(, m[td]);
}
while(res.size() > && res[] == '')
res = res.substr();
return res;
}
};
[leetcode] 405. Convert a Number to Hexadecimal的更多相关文章
- 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 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_Easy tag: Bit Manipulation
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- 405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果 ...
- 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 ...
- [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- Leetcode: Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...
随机推荐
- C#- Winform调用BAT例子
前段时间在工作的时候需要用到,百度了好久后找,可是找到了又希望调用的时候窗体不要显示出来. proc.StartInfo.CreateNoWindow = true; proc.Start ...
- matlab inpolygon 判断点在多边形内
如何判断一个点在多边形内部? xv= [0 3 3 0 0]; %x坐标 yv= [0 0 3 3 0];%y坐标 x=1.5; y=1.5; in=inpolygon(x,y,xv,yv) plot ...
- UITableview 中获取非选中的cell
实现效果如图: 在cell中有一个button,选中cell改变button的选择状态 yes,选中另外一个cell,别的cell中的button选择状态变成false. //获取当前可显示的cell ...
- android驱动[置顶] 我的DIY Android之旅--驱动并控制你的Android开发板蜂鸣器
改章节个人在深圳游玩的时候突然想到的...这几周就有想写几篇关于android驱动的博客,所以回家到之后就奋笔疾书的写出来发布了 这些天一直在想Android驱动框架层的实现,本文借助老罗教师的博客和 ...
- material-design-library
https://github.com/DenisMondon/material-design-library
- HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决
又是折腾了一天才解决的问题,网上关于这个问题的资料不多,希望写出来能帮到有需要的人. 之前无论怎么设置charset都不起作用, 后来看了这篇文章 才发现MultipartEntityBuilder有 ...
- Linux dd——备份命令
Linux学习笔记之备份命令dd 功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换.可以用该命令实现DOS下的diskcopy命令的作用.先用dd命令把软盘上的数据写成硬 ...
- windows 7文件共享方法
现在家里有几台电脑是很常见的,把家里的几台电脑组成局域网,共享文件.联机游戏是非常有必要的,但是组建局域网又是很多朋友永远的痛,需要设置工作组.设置登录方式.打开服务等等,时常还会出点小问题而无法共享 ...
- 微软Hololens学院教程- Holograms 100: Getting Started with Unity【微软教程已经更新,本文是老版本】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
- DBCP数据源
DBCP数据源是Apache软件基金组织下的开源连接池实现,需要两个jar文件:Commons-dbcp.jar 连接池的实现和Commons-pool.jar 连接池实现的依赖库