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. 刚制作完的SAP Sybase ASE15.7 [Sybase Central] 客户端

    支持32位和64位windows系统.无需其它任何包.原汁原味. 支持的数据库版本,应该从15.0开始都支持. 下载地址: http://download.csdn.net/detail/iihero ...

  2. 【C++深入浅出】设计模式学习之观察者模式

    前言 前两天学习了weak_ptr以后还是不甚明了,一则需要实际应用去锤炼,二来就是不懂观察者模式. 正文 观察者模式又叫发布-订阅模式,定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对 ...

  3. ZZTHX-注意点

    遇到刷卡器加密错误和后台解密不了的问题确实不太好解决,也有加密后的数据返回,可是后台总是解密不了.在这里我首先要感谢一下我的同事,在他们的帮助下,项目顺利完成了.有以下注意点现汇总如下: 1.密码加密 ...

  4. ant中调用外部ant任务的两种方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  5. How to add “Maven Managed Dependencies” library in build path eclipse

    If you have m2e installed and the project already is a maven project but the maven dependencies are ...

  6. 理解 auto_ptr<T>

    1.auto_ptr<T>解决什么问题? 在堆上获取的资源,客户可能会忘记delete,或者由于异常没有执行到delete,导致资源泄漏.在栈上分配的对象,有个特点,不论出现什么情况,超出 ...

  7. Fatal error: Using $this when not in object context in 解决方法

    Fatal error: Using $this when not in object context in 解决方法 粗心造成的错误 $this 只存在于下面情况 $obj = new object ...

  8. cocos2d-x3.1.1 step by step 学习笔记1----- 软件的安装与HelloWord

    1:软件安装 系统:WIn7 软件:VS2012.cocosd-x3.1.1.python2.6.7.doxygen <1>首先安装VS2012,安装也是傻瓜式安装, <2>然 ...

  9. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

  10. Linux HugePages及MySQL 大页配置

    http://blog.csdn.net/dba_waterbin/article/details/9669929       ㈠ HugePages简介             HugePages是 ...