Topic:
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 ele ment is distinct.
Effect:
An array of type int is given to determine if the array contains duplicate numbers. If any of the values occur at least two times in the function, your array should return TRUE if each value is single, then return false.
Ideas:
At first I used the same idea of the previous one to judge the number of letters, using a 10-bit array to record the number of occurrences of the 0~9, and later running said there were negative numbers ... So add the -9~-1 9 numbers, the array to 19 bits, run and found that there are huge numbers ... Instead of the single digit number I think, it's more than the array's ability to withstand, and at first it's not clear = =.
So change a way of thinking, first the array of numbers sorted, sorted after the contents of the array is in order, if there is the same value, it must be adjacent to the arrangement, so as long as the traversal of the array to check if there are two adjacent values equal. This time finally AC, looked at the discuss the most hot method, as I thought, too happy.
There are a number of ways to sort the Java array from the sorting function, you can also use some sort of algorithm, you can refer to this blog: http://blog.csdn.net/fengyifei11228/article/details/2623980, writing is quite complete.
Code (JAVA):
Public class solution { Public Boolean containsduplicate(int[] nums) {arrays.sort (nums);//First order in array, reference: http://blog.csdn.net/fengyifei11228/article/details/2623980 for(inti =0; I < nums.length-1; i++) {if(Nums[i] = = Nums[i +1])return true;//loop to determine if there are two identical numbers after sorting}return false; }}
All rights reserved: Http://blog.csdn.net/cloudox_
Leetcode notes: 217. Contains Duplicate