• leetcode 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:

    1. All letters in hexadecimal (a-f) must be in lowercase.
    2. 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.
    3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
    4. You must not use any method provided by the library which converts/formats the number to hex directly.

    Example 1:

    Input:
    26
    
    Output:
    "1a"
    

    Example 2:

    Input:
    -1
    
    Output:
    "ffffffff"

    10进制转换到16进制,其中负数用补码的形式处理

    其实,计算机内都是用补码的形式进行计算的。被题目晃了一下。

    下面是我愚笨的代码,把10进制转换到二进制然后转换到负数的二进制,然后再转换到16进制。

    class Solution {
    public:
        char get(int x) {
            if (x < 10) return char(x + '0');
            else {
                switch(x){
                    case 10: return 'a';
                    case 11: return 'b';
                    case 12: return 'c';
                    case 13: return 'd';
                    case 14: return 'e';
                    case 15: return 'f';
                }
            }
        }
        string toHex(int num) {
            if (num == 0) return "0";
            int x = num;
            if (x < 0) x = -x;
            vector<int> v;
            while (x > 0) {
                v.push_back(x%2);
                x /= 2;
            }
            while (v.size() < 32) {
                v.push_back(0);
            }
            string s = "";
            if (num < 0) v[31] = 1;
            else {
                int mark = 0;
                for (int i = 31; i >= 0; i -= 4) {
                    int x = 0;    
                    for (int j = 3; j >= 0; --j) {
                        if (v[i - j] == 1)x += (int)pow(2.0,3 - j);        
                    }
                    if (x == 0 && !mark) continue;
                    mark = 1;
                    s += get(x);
                }
                return s;
            }
            for (int i = 30; i >= 0; --i) {
                v[i] = 1 - v[i];
            }
            for (int i = 0; i < 30; ++i) {
                if (i == 0) {
                    v[i] ++;
                    if (v[i] >= 2) v[i + 1] ++,v[i] = 0;
                    else break;
                } else {
                    if (v[i] >= 2) v[i] = 0, v[i + 1]++;
                    else break;
                }
            }
            int mark = 0;
            for (int i = 31; i >= 0; i -= 4) {
                int x = 0;    
                for (int j = 3; j >= 0; --j) {
                    if (v[i - j] == 1) x += (int)pow(2.0,3 - j);        
                }
                if (x == 0 && !mark) continue;
                mark = 1;
                s += get(x);
            }
            return s;
        }
    };

    其实计算机中数字本身就是补码的形式表示的,我们只需要直接将其装换到16进制就可以了,转换的思路就是每4个二进制位进行枚举,将这4个二进制转换到0~15就ok了

    class Solution {
    public:
        string toHex(int num) {
            if(0==num) return "0";
            string res;
            const string HEX = "0123456789abcdef";
            int count=0;
            while(num && 8>count++){
                res = HEX[(num & 0xf)] + res;
                num>>=4;
            }
            return res;
        }
    };
  • 相关阅读:
    java项目和web项目路径的区别
    ModelAndView跳转
    struts漏包和tomcat上传问题
    映射文件的路径写错的出现的错误
    hibernate二级缓存
    find命令小结
    【转】Python的XML-RPC简介
    Python的类实例方法,类方法,类静态方法
    itertools模块速查
    Python中处理时间 —— time模块
  • 原文地址:https://www.cnblogs.com/pk28/p/7307072.html
Copyright © 2020-2023  润新知