Different result in run and submit.Anyone could help me?


  • 0
    A

    The test case is [1,1,2,2,2,1].In the run the result is 3 but in submit the result is 6.Anyone could help me?
    Here is my code

    class Solution {
    public:
        int findShortestSubArray(vector<int>& nums) {
            if(nums.size()<2) return nums.size();
            int count[50000];
            map<int,int> num2index;
            int maxcount = 0,mindis = 0;
            for(int i  =0;i<nums.size();++i){
                if(num2index.count(nums[i]) == 0){
                    num2index[nums[i]] = i;
                    count[nums[i]]++;
                }else{
                    count[nums[i]]++;
                }
                if(count[nums[i]]>maxcount){
                    maxcount = count[nums[i]];
                    mindis  = i - num2index[nums[i]]+1;
                }else if(count[nums[i]]==maxcount){
                    int dis = i - num2index[nums[i]]+1;
                    if(dis<mindis){
                        maxcount = count[nums[i]];
                        mindis  = dis;
                    }
                }                        
            } 
            return mindis;
        }
    };
    

  • 0
    A

    @a698493098 The reason is count is not initialized.int count[50000] should be replaced by int count[50000] = {0};


Log in to reply
 

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