Majority Element --Time Limit Exceeded--Why?


  • 0
    E
    public class Solution {
    public int majorityElement(int[] nums) {
      		int len = nums.length;
    	int flag = 1;
    	for (int i = 0; i < len; i++) {
    		for (int j = i + 1; j < len; j++) {
    			if (nums[j] == nums[i])
    				flag++;
    			if (flag > len / 2)  return nums[i];
    		}
    		flag = 1;
    	}
    	return 0;}}

Log in to reply
 

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