A trivial beginners solution , but getting an error in last test case for value -2147483648. Any help appreciated.


  • 0
    S
    class Solution {
    public:
    char hex_map[16];
        Solution(){
    		hex_map[0] = '0';
    		hex_map[1] = '1';
    		hex_map[2] = '2';
    		hex_map[3] = '3';
    		hex_map[4] = '4';
    		hex_map[5] = '5';
    		hex_map[6] = '6';
    		hex_map[7] = '7';
    		hex_map[8] = '8';
    		hex_map[9] = '9';
    		hex_map[10] = 'a';
    		hex_map[11] = 'b';
    		hex_map[12] = 'c';
    		hex_map[13] = 'd';
    		hex_map[14] = 'e';
    		hex_map[15] = 'f';
        }
    	string ones_twos_complement(string str) {
    		for (int i = 0; i < str.length(); i++) {
    			if (str[i] == '0')
    				str[i] = '1';
    			else
    				str[i] = '0';
    		}
    		//cout << str << endl;
    		//twos' complement
    		for (int i = str.length() - 1; i >= 0; i--) {
    			if (str[i] == '0') {
    				str[i] = '1';
    				break;
    			}
    			str[i] = '0';
    		}
    		//cout << endl << str;
    		return str;
    	}
    
    
    	string decimal_to_binary(int num) {
    		bool negative = false;
    		if (num < 0)
    			negative = true;
    
    		num = abs(num);
    		string str = "";
    		//convert to binary
    		while (1) {
    			int r = num % 2;
    			num /= 2;
    			str += '0' + r;
    			if (num == 1 || num==0)
    				break;	
    		}
    		str += '0' + num;
    		num = 0;
    		int x = str.length();
    		if (str.length() < 32) {
    			for (int i = 0; i < 32 - x; i++) {
    				str += '0';
    			}
    		}
    
    		//cout << str.length();
    		std::reverse(str.begin(), str.end());
    		//to find ones' and twos' complement.
    		if(negative)
    			str = ones_twos_complement(str);
    	
    		string result = "";
    		for (int i = 0; i < 32; i+=4) {
    			int temp=0;
    			int power = 3;
    			for (int j = i; j < i + 4; j++) {
    				if (str[j] == '1') 
    					temp += pow(2, power);
    				power--;
    			}
    			result += map(temp);
    		}
    
    		if (!negative) {
    			reverse(result.begin(), result.end());
    			for (int i = result.length() - 1; i >= 0; i--) {
    				if (result[i] == '0')
    					result.resize(result.length() - 1);
    				else
    					break;
    			}
    			reverse(result.begin(), result.end());
    		}
    		
    		return result;
    	}
    	char map(int num) {
    		
    		return hex_map[num];
    	}
    	string toHex(int num) {
    	    string st;
    	   if(num!=0)
    		    st= decimal_to_binary(num);
    	   else
    		    st='0';
    		return st;
    	}
    
    };
    
    

  • 0
    S

    @sujiths52 Fixed it by changing int to unsigned int I dont understand why that worked though.


  • 0
    M

    This line

    num = abs(num);

    is the offender. it will ignore -2^31, because it doesn't have a corresponding abs value

    so when it returns, it still is -2^31, not 2^31 which is what you intend it to be


  • 0
    M

    add these two line in the begging of the code.

    if (num == INT_MIN) return "80000000";
    if (num == INT_MAX) return "7fffffff";


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.