1. Runtime Distribution
2. Submission Details
3. Description
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.
4. Example
input 2,3,1,0,2,5,3
output 2 or 3
5. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
public boolean containsDuplicate(int[] nums) { int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int b: nums){ if(bmax){ 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; }
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
public class LeetCode0217 { public boolean containsDuplicate(int[] nums) { int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int b: nums){ if(bmax){ 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)); } }
[/restab]
[/restabs]
Comments | NOTHING