c solution 6ms


  • 0
    T
    int majorityElement(int* nums, int numsSize){
        int i;
        if(numsSize==1||numsSize==2)
            return *nums;
        for(i=numsSize/2;i>0;i--){
            if(nums[0]!=nums[i]){
                nums[i]=nums[i]^nums[1];
                nums[1]=nums[i]^nums[1];
                nums[i]=nums[i]^nums[1];
                break;
            }
        }
        if(i==0)
            return *nums;
        return majorityElement(nums+2,numsSize-2);
    }
    

    It will always have majorityElement if delete 2 different elements.


Log in to reply
 

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