标签:
1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.Collection; 4 import java.util.HashMap; 5 import java.util.Set; 6 7 public class Solution { 8 public int MoreThanHalfNum_Solution(int [] array) { 9 if(array==null||array.length==0){ 10 return 0 ; 11 } 12 if(array.length==1){ 13 return array[0] ; 14 } 15 int most=0 ; 16 HashMap<Integer,Integer> m = new HashMap<Integer,Integer>() ; 17 for(int i = 0 ; i < array.length ;i++){ 18 if(!m.containsKey(array[i])){ 19 m.put(array[i], 1) ; 20 } 21 else{ 22 int temp = m.get(array[i]); 23 m.put(array[i], temp+1) ; 24 } 25 26 } 27 Set<Integer> a = m.keySet() ; 28 for(int temp : a){ 29 if(m.get(temp)>(array.length/2)){ 30 most = temp ; 31 } 32 } 33 return most ; 34 } 35 }
标签:
原文地址:http://www.cnblogs.com/huntertoung/p/4803147.html