顿搜
飞过闲红千叶,夕岸在哪
类目归类
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
input 2,3,1,0,2,5,3
output 2 or 3
public boolean containsDuplicate(int[] nums) {
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int b: nums){
if(b<min){
min=b;
}
if(b>max){
max=b;
}
}
boolean record[] = new boolean[max-min+1];
for(int num : nums){
if(record[num-min]){
return true;
}else{
record[num - min] = true;
}
}
return false;
}public class LeetCode0217 {
public boolean containsDuplicate(int[] nums) {
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int b: nums){
if(b<min){
min=b;
}
if(b>max){
max=b;
}
}
boolean record[] = new boolean[max-min+1];
for(int num : nums){
if(record[num-min]){
return true;
}else{
record[num - min] = true;
}
}
return false;
}
public static void main(String[] args) {
LeetCode0217 leetcode = new LeetCode0217();
int[] nums = {1,3,5,2,6,9,5};
System.out.println(leetcode.containsDuplicate(nums));
}
}