Contains duplicate,contains Duplicate ii,contains Duplicate III

Source: Internet
Author: User

217. Contains Duplicate

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.

Determines whether the array contains duplicate elements.

A lot of ideas, violence to find each element, sort after the search, map to count the number of occurrences of each element, set does not contain duplicate elements.

With a little trick, set can be used to remove duplicate elements from the array, and there are sorting functions. Map can count the number of occurrences of each element, as well as the sorting function

class Solution {public:    bool containsduplicate (vector<int>& nums) {        returnset<int> (Nums.begin (), Nums.end ()). Size () <  Nums.size ();    }};
219. Contains Duplicate II

Given an array of integers and a integer K, find out whether there is II distinct indices i and C16>j in the array such that nums[i] = Nums[j] and the difference between I and Jare at most k.

Similar to the previous one, but every time the data range of a repeating element is changed to K, we want to maintain a sliding window of size K for ourselves.

About the sliding window, you can put the data inside the window into a container (set,map,vector, etc.), you can also use subscript to maintain a range of expressions, different topics flexible choice

About the return value of insert in Set/unordered_set, which returns a Pair<set/uset iterator, Bool> a second Boolean value of TRUE indicates a successful insertion, or false to indicate that the element is already available, to view the CPP Reference

classSolution { Public:    BOOLContainsnearbyduplicate (vector<int>& Nums,intk) {unordered_set<int>windows;  for(intI=0; I<nums.size (); + +i) {            if(i>k) {Windows.erase (Nums[start-1]); }                        if(Windows.insert (Nums[i]). Second = =false){                return true; }        }                return false; }};
Contains Duplicate III

Given an array of integers, find out whether there is II distinct indices i and J in the array such th At the difference between nums[i] and Nums[j] are at most T and the difference between I and J I S at the most K.

The problem-solving framework is still a sliding window, but it is not a matter of judging whether there are duplicate elements in the window, but rather the difference between the two numbers in the window. If the direct use of violence to determine whether the difference between the two number of the window is less than or equal to T, need O (k*k) time complexity, there is a total of n-k windows, if you follow this idea to write code, will time out. So find another way. Violence is not a purpose to enumerate all the number pairs, and now we know that the need for the two number of Windows to meet the |a-x|<=t relationship, that is, a-t =< x <= a+t, so we can use the idea of finding, each time to throw a window into a data, check whether there is [a-t,a+ T] The data within this range is in the window.

Assuming that the data in the Set storage window is used, the Lower_bound function can be used to find the first data greater than or equal to a-t in the O (lgk) time, and then check if the found data is less than or equal to a+t.

classSolution { Public:    BOOLContainsnearbyalmostduplicate (vector<int>& Nums,intKintt) {Set<int>windows;  for(intI=0; I<nums.size (); + +i) {                        if(i>k) {Windows.erase (nums[i-k-1]); } Auto ITER= Windows.lower_bound (nums[i]-t); if(iter! = Windows.end () && *iter <= Nums[i] +t) {                return true;        } windows.insert (Nums[i]); }        return false; }};

The key word for these three questions is the relationship between the sliding window, the data in the window

Contains duplicate,contains Duplicate ii,contains Duplicate III

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.