Given OS and Answer OS are not running on the same system


  • 0
    A

    The OS they give us in this problem is 16 bits whereas the answers are run on a 32 bit system. An integer on a 16 bit OS is 16 and 32 on 32.
    The following solution did not work because sizeof(int) was evaluated to 16:

    class Solution {
    public:
        int totalHammingDistance(vector<int>& nums) {
            int retVal=0;
            for(int i=0; i<sizeof(int); i++){
                int numZ=0, numO=0;
                for(int j=0; j<nums.size(); j++){
                    if(nums[j]>>i&1) numO++;
                    else numZ++;
                }
                retVal+=numO*numZ;
            }
            return retVal;
        }
    };
    

    whereas this did:

    class Solution {
    public:
        int totalHammingDistance(vector<int>& nums) {
            int retVal=0;
            for(int i=0; i<32; i++){
                int numZ=0, numO=0;
                for(int j=0; j<nums.size(); j++){
                    if(nums[j]>>i&1) numO++;
                    else numZ++;
                }
                retVal+=numO*numZ;
            }
            return retVal;
        }
    };
    

Log in to reply
 

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