解题思路:
ac代码:
import java.util.hashmap; import java.util.hashset; import java.util.map; import java.util.scanner; import java.util.set; public class main{ /** * @param args */ static int n; static set<integer> set = new hashset<integer>() ; static int a[]=new int[1000000+2]; static map<integer,integer> map=new hashmap<integer, integer>(); public static void main(string[] args) { // todo auto-generated method stub scanner scan=new scanner(system.in); n=scan.nextint(); for(int i = 0 ; i < n ; i++){ set.add(a[i] = scan.nextint() ) ; } int size = set.size() ; //计算不同知识点的个数 int start = 0 , end = 0 , sum = 0 ; int res = n ; for(;;){ while(end < n && sum < size){ integer cnt = map.get(a[end]) ; if(cnt == null){ sum++ ; map.put(a[end] , 1) ; } else map.put(a[end] , cnt+1) ; end++ ; } if(sum < size) break ; res = math.min(end - start , res) ; int cnt = map.get(a[start]) ; if(cnt == 1){ map.remove(a[start]) ; sum-- ; } else map.put(a[start] , cnt-1) ; start++ ; } system.out.println(res) ; } }
以上就是分享一个java计算的实例的详细内容。