其他分享
首页 > 其他分享> > 697. 数组的度 - 应该还可以优化!!!!

697. 数组的度 - 应该还可以优化!!!!

作者:互联网

 

 

import java.util.Arrays ;

class Solution {
    
    
    
    public int findShortestSubArray(int[] nums) {
        
    
        int size = nums.length;
        int max=-1;
        for(int i =0;i<size ;i++){
            if(nums[i]>max){
                max=nums[i];
            }
        }
        
        int[][] mtx =new int[max+1][3];
        
        for(int i=0;i<size;i++){
            int value = nums[i];
            int index = i+1;
           int[] startEndCount = mtx[value];
            if(startEndCount[0]==0){
                startEndCount[0]=index;
            }else{
                startEndCount[1]=index;
            }
            startEndCount[2]= startEndCount[2]+1;
        }
        
        int maxCount=-1;
        int returnSize = 0;
        for(int i=0;i<max+1;i++){
            if(mtx[i][2]>maxCount){
                maxCount=mtx[i][2];
                returnSize = mtx[i][1]-mtx[i][0]+1;
            }else if(mtx[i][2]==maxCount&&(mtx[i][1]-mtx[i][0]+1)<returnSize){
               returnSize= mtx[i][1]-mtx[i][0]+1;
            }
        }
    
        
        return returnSize<=0?1:returnSize;
        
    }
    
}

  

标签:mtx,697,nums,int,max,数组,maxCount,优化,findShortestSubArray
来源: https://www.cnblogs.com/leodaxin/p/11253480.html