标签:font problems -o length 最大值 ems ++ style info
nums.length
在1到50,000区间范围内。nums[i]
是一个在0到49,999范围内的整数。1 class Solution { 2 public int findShortestSubArray(int[] nums) { 3 if(nums == null || nums.length == 0) return 0; 4 int minLen = nums.length; 5 int max = 0; 6 int min = 0; 7 for(int x : nums){ 8 if( x > max ) max =x; 9 if( x < min ) min = x; 10 } 11 12 int[] freq = new int[max - min +1]; 13 max = 0; 14 for(int x : nums){ 15 freq[x-min]++; 16 if(freq[x-min] > max) max = freq[x-min]; 17 } 18 if(max == 1) return 1; 19 20 for(int i = 0; i < freq.length; i++){ 21 if(freq[i] == max){ 22 int l = 0; 23 int r = nums.length-1; 24 while(l < r){ 25 if(nums[l] == i+min ){ 26 if(nums[r] == i+min){ 27 minLen = Math.min(r-l+1,minLen); 28 break; 29 } 30 else r--; 31 } 32 else l++; 33 } 34 } 35 } 36 return minLen; 37 } 38 }
标签:font problems -o length 最大值 ems ++ style info
原文地址:https://www.cnblogs.com/leechee9/p/12120635.html