My C++ code with 23ms


  • 1
    H
    	if (nums.size() < 3)
    		return nums[0];
    	vector<int> a;
    	double half = (double)nums.size() / 2 + 0.5;
    	int b = half;
    	int d = half;
    	int countsum = 0;
    	for (int i = 0; i < nums.size(); i++)
    	{
    		int c = 0;
    		for (int j = 0; j < a.size(); j++)
    		{
    			if (nums[i] == a[j])
    			{
    				c = 1;
    				break;
    			}
    		}
    		if (c == 0)
    		{
    			a.push_back(nums[i]);
    			int count = 1;
    			for (int j = i + 1; j < nums.size(); j++)
    			{
    				if (nums[i] == nums[j])
    				{
    					count++;
    					if (count == b)
    						return nums[i];
    				}
    				
    			}
    			countsum += count;
    			b = d +1 - countsum;
    		}
    	}
    }
    

    };


Log in to reply
 

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