Leetcode notes: 217. Contains Duplicate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.