剑指offer——数组中重复的数字
题目描述:找出数组中重复的数字
题目解析:可以将数组进行排序,然后从头到尾遍历排好序的数组就可以了;还可以使用HasnMap来记录数组中每个元素的状态,如果扫描到的元素在HashMap中的状态为“true”,那么就找到一个重复的数字,复杂度为O(n)。import java.util.HashMap;
public class RepeatArrayNumber {
public static void result(int[] a){
HashMap<Integer, String> map = new HashMap<Integer, String>();
for(int i=0;i<a.length;i++){
if(map.get(a[i]) == "true"){
System.out.println(a[i]);
}
else
map.put(a[i], "true");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]={2,3,1,0,2,5,3};
result(a);
}
}