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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. [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 ...

  5. 405 Convert a Number to Hexadecimal 数字转换为十六进制数

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意:    十六进制中所有字母(a-f)都必须是小写.    十六进制字符串中不能包含多余的前导零.如果 ...

  6. 405. Convert a Number to Hexadecimal

    ..感觉做的很蠢. 主要就是看负数怎么处理. 举个例子,比如8位: 0111 1111 = 127 1111 1111 = -1 1000 0000 = -128 正常情况1111 1111应该是25 ...

  7. 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 ...

  8. [LeetCode] Convert a Number to Hexadecimal 数字转为十六进制

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  9. Leetcode: Convert a Number to Hexadecimal

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...

随机推荐

  1. XMPP——Smack[6]离线消息和离线文件的实现

    终篇,三天所学所用,也就这些,如果需要大家要自己去查资料研究研究,功能其实可以很强大的 可惜界面做得不好,一大短处,从大一迄今没整好,主要是个人审美不行,哎 毕业季呀毕业季,明天摆摊卖书,再半月就可能 ...

  2. linux设置tomcat开机自动启动

    1.修改/etc/rc.d/rc.local,使用vi /etc/rc.d/rc.local 命令2.在/etc/rc.d/rc.local文件最后添加下面两行脚本 export JAVA_HOME= ...

  3. 作为平台的Windows PowerShell(一)

    除了作为一种脚本语言外,Windows PowerShell被多种应用程序使用.这是因为Windows PowerShell引擎可以被托管在一个应用程序内部.这篇博文和下一篇博文将会处理在C#应用程序 ...

  4. 【转】使用junit进行单元测试(初级篇)

    转自:http://blog.csdn.net/andycpp/article/details/1327147 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在 ...

  5. Andropid自己定义组件-坐标具体解释

    在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄非常清楚. 如今把整个总结一下. 事实上仅仅要把以下这张图看明确就没问题了. watermark/2/text/aHR ...

  6. 父 shell,子 shell ,export 与 变量传递

    http://blog.csdn.net/dreamcoding/article/details/8519689/ http://caoruntao.iteye.com/blog/1018656

  7. 学习笔记之Java程序设计实用教程

    Java程序设计实用教程 by 朱战立 & 沈伟 学习笔记之JAVA多线程(http://www.cnblogs.com/pegasus923/p/3995855.html) 国庆休假前学习了 ...

  8. 101个直接可以拿来用的JavaScript实用功能代码片段(转)

    1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^x00-xff ...

  9. Mini2440 DM9000 驱动分析(一)

    Mini2440 DM9000 驱动分析(一) 硬件特性 Mini2440开发板上DM9000的电气连接和Mach-mini2440.c文件的关系: PW_RST 连接到复位按键,复位按键按下,低电平 ...

  10. 用java程序模拟网站的登录以及文件批量上传

    import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; ...