[LeetCode]405. Convert a Number to Hexadecimal(32位有符号整数转化为十六进制)

405. Convert a Number to Hexadecimal

原题链接
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
题目大意:
给一个整数,写出它的十六进制,对于负整数,可以用二进制补码方法解决。

Note:

  • All letters in hexadecimal (a-f) must be in lowercase.
  • 十六进制(a-f)中的所有字母必须为小写。
  • The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character ‘0’; otherwise, the first character in the hexadecimal string will not be the zero character.
  • 十六进制字符串不能包含额外的前导0。 如果数字为零,则由单个零字符’0’表示; 否则,十六进制字符串中的第一个字符将不为零字符。
  • The given number is guaranteed to fit within the range of a 32-bit signed integer.
  • 给定的数字保证在32位有符号整数的范围内。
  • You must not use any method provided by the library which converts/formats the number to hex directly.
  • 您不得使用库中提供的任何方法将数字直接转换/格式化为十六进制。

思路:

  • 首先要读懂题意,将整数转化为十六进制,包括非负整数
  • 二进制补码的方法要清楚,补码 = 原码取反 + 1点击查看介绍
  • 题目给出可以用二进制补码的方法解决,其实并不是让你将十进制转化为二进制,再用二进制补码求解,最后转化为十六进制。十六进制和二进制相似,均可以补码 = 原码取反 + 1
  • 32位有符号整数范围 -2147483648 ~ +2147483647,注意是不对称的,|TMin| = |TMax|+1 所以TMin没有与之对应的正数,这点非常重要!
  • 接下来考虑算法,分情况讨论num=0||num<0||num>0||num=-2147483648

代码如下:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Solution {
public:
    string toHex(int num) {//3ms
        if(num == 0)
            return "0";
        if(num == -2147483648)//由于补码的范围是不对称的 |TMin| = |TMax|+1  所以TMin没有与之对应的正数
            return "80000000";
        string res = "";
        int NUM = abs(num);//取绝对值
        while(NUM != 0){
            int temp = NUM%16;
            res = Hex[temp] + res;
            NUM = NUM/16;
        }
        if(num < 0){
            res = help(res);
        }
        return res;
    }
    string help(string s){//用于原码取反+1
        int sL = s.size()-1,add=1;
        string Res = "";
        while(sL>=0 || add>0)
        {
            int cur=add;
            cur+=sL>=0?(15-reInt(s[sL--])):0;//补码 = 原码取反+1
            add=cur/16;//用来判断是否有进位
            cur%=16;
            Res=Hex[cur]+Res;
        }
        if(s.size()<8){
            for(int i=1; i<=8-s.size(); i++)
                Res = 'f' + Res;
        }
        return Res;
    }
    int reInt(char c){//用于返回十六进制中单个字符代表的数字
        for(int i=0; i<=15; i++)
            if(c == Hex[i])
                return i;
        return -1;
    }

private:
    char Hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
};
int main()
{
    Solution S;
    int num = -2147483648;
    cout << "num to Hexadecimal is " << S.toHex(num) << endl;

    return 0;
}

提交后查看别人的优秀解法,我还没看懂0.0,先贴出来

class Solution {
public:
    string toHex(int num) {
        string result = "";
        if (num == 0) {
            return "0";
        }
        int count = 0;
        while (num != 0 && count < 8) {
            result = hex[(num & 0xf)] + result;
            num >>= 4;
            count++;
        }
        return result;
    }

private:
    string hex = "0123456789abcdef";

};

自己的理解:

  • << >> 移位运算
  • 《 k 左移 向左移动k位,丢弃最高的k位,并在右端补k个0
  • 》k 有算数右移和逻辑右移
  • 逻辑右移 num向右移k位,在左端补k个0
  • 几乎所有的编译器对有符号数都支持算数右移
  • 算数右移 num向右移k位,往左端补k个最高有效位的值
  • 如果有人能看懂,欢迎在下面留言,谢谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值