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. Java循环语句 for

    语法: 特点:相比 while 和 do...while 语句结构更加简洁易读 例如,输出 1000 遍"我爱慕课网",使用 for 的实现代码为: 需要留心的几个小细节: 1. ...

  2. 使用Windows的分析等待链(analyze wait chain)来诊断没用响应的应用

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用Windows的分析等待链(analyze wait chain)来诊断没用响应的应用.

  3. Ms SQL Server 约束和规则

    一.SQL约束 约束定义关于列中允许值的规则,是强制完整性的标准机制. 使用约束优先于使用触发器.规则和默认值.查询优化器也使用约束定义生成高性能的查询执行计划. 1:类型 约束的类型一共分三种 域约 ...

  4. SQL Server 127个SQL server热门资料汇总

      SQL Server 127个SQL server热门资料汇总     最近有许多关于如何学习SQLSERVER的问题,其实新手入门的资源和贴子很多,现在向大家隆重推荐经过精心整理的[SQLSer ...

  5. Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

    D. Phillip and Trains Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/ ...

  6. WINAPI 变量(2861个)

    WINAPI 变量(2861个)   这是从 c:\Program Files\Windows Kits\8.1\Include\um\WinUser.h 这个文件 中提取的 CTRL+F 查看变量所 ...

  7. 安装完zend server后,无法访问http://localhost:10081/ZendServer/的解决办法

    安装完ZendServer后,默认会设置http://localhost:10081/ZendServer/为ZendServer的后台管理页面, 但对于ZendServer5.0.2(其它版本未知) ...

  8. 《高级Perl编程》 读书笔记

    http://blog.chinaunix.net/uid-20767124-id-1849881.html

  9. java_泛型(设置通配符下限)

    package ming; import java.util.ArrayList; import java.util.Collection; import java.util.List; /* * 返 ...

  10. iOS 中的 block 是如何持有对象的

    Block 是 Objective-C 中笔者最喜欢的特性,它为 Objective-C 这门语言提供了强大的函数式编程能力,而最近苹果推出的很多新的 API 都已经开始原生的支持 block 语法, ...