The test case is not so strong


  • 0

    following code can be accepted in this problem. But its' answer of case [1,2,2,3,3,4,4,5,1,1,1,1] is not the same as the expected answer.

    public class Solution {
    int num[] = new int[3];
    int time[] = new int[3];
    int n = 0;
    public void add(int x){
        int index = -1;
        for(int i = 0; i < n; ++i)
            if(num[i] == x)
                index = i;
        if(index == -1){
            if(n < 3){
                num[n] = x;
                time[n] = 1;
                ++n;
            }else{
                int min = 0;
                for(int i = 1; i < n; ++i)
                    if(time[i] < time[min])
                        min = i;
                num[min] = x;
                time[min] = 1;
            }
        }else time[index] += 1;
            
    }
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        if(nums.length == 0)
            return res;
        for(int i = 0; i < nums.length; ++i)
            add(nums[i]);
        for(int i = 0; i < n; ++i)
            if(time[i] > nums.length / 3)
                res.add(num[i]);
        return res;
    }}

  • 0

    Thanks, you are right, the answer should be [1] but your code return []. I have added your test case.


Log in to reply
 

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