题解 | #数组中重复的数字#
数组中重复的数字
https://www.nowcoder.com/practice/6fe361ede7e54db1b84adc81d09d8524
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型一维数组
* @return int整型
*/
public int duplicate (int[] numbers) {
if(numbers == null || numbers.length == 0) {
return -1;
}
//暴力数组字典:
byte pSet[] = new byte[10000];
for (int i = 0; i < numbers.length; i++) {
if (pSet[numbers[i]] > 0) {
return numbers[i];
} else {
++pSet[numbers[i]];
}
}
return 0;
}
}
暴力美学,直接拿数组下标作为映射,数组值为重复次数,这样就符合空间和时间都o(n)的要求了


腾讯云智研发成长空间 294人发布