[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 ...
随机推荐
- android获取mac地址方法
http://www.cnblogs.com/xioapingguo/p/4037513.html 网上找的,记录一下 public static String getMacAdress(){ Wif ...
- 【转】与BT下载相关的概念
1. DHT DHT全称叫分布式哈希表(Distributed Hash Table),是一种分布式存储方法.在不需要服务器的情况下,每个客户端负责一个小范围的路由,并负责存储一小部分数据,从而实现整 ...
- mysql 字符集设置查看
1.列出MYSQL支持的所有字符集: SHOW CHARACTER SET; 2.当前MYSQL服务器字符集设置 SHOW VARIABLES LIKE 'character_set_%'; 3.当前 ...
- 升级ADT22.6后,Android模拟器无法创建
这 两天,在社区里看到有小伙伴们反应,自己在Eclipse下无法创建Android模拟器的问题.起初,自己也没太在意,我一直使用的是 Genymotion模拟器.然后,问题不解决,总有那么一天会让自己 ...
- sql server char nchar nvarchar varchar之間的區別
char存储固定长度的字符串,最大长度为8000个字节. varchar存储可变长度的字符串.最大长度为8000个字节. nchar存储固定长度的Unicode字符串,最大长度为4000个字符. nv ...
- css Hack,用IE11模拟测试的,条件注释要找真IE去测,模拟的无效
<!DOCTYPE html> <!--[if lt IE 7 ]> <html class="ie6 ie"> <![endif]--& ...
- endif、endforeach
<?php if ($a == 5): ?> <div>等于5</div><?php elseif ($a == 6): ?> <div> ...
- [COCOS2DX]第一个开源项目的部署和运行<win32版本>
1.无法加载项目: 1.1 按照之前的方法创建一个名为FirstDemo的项目,并将下载到的源代码包全部拷贝到FirstDemo中 1.2 点击demo.sln启动项目: 修改属性: 2.无法打开“g ...
- Maven项目中如何添加日志
- JS类型(1)_JS学习笔记(2016.10.02)
js类型 js中的数据类型有undefined,boolean,number,string,null,object等6种,前5种为原始类型(基本类型),基本类型的访问是按值访问的,就是说你可以操作保存 ...