Duplicate element Leetcode 219. II

Description:

  First, it is an Easy question, my day! But to understand the meaning of problems or a lot of pit ~

Subject description:

  Given an integer array and an integer k, if there are two different indices i and j is determined array, such nums [i] = nums [j ], and the difference of i and j is the maximum absolute value K is .

  Example 1:

  Input: nums = [1,2,3,1], k = 3
  Output: true


  Example 2:

  Input: nums = [1,0,1,1], k = 1
  Output: true


  Example 3:

  Input: nums = [1,2,3,1,2,3], k = 2
  Output: false

understanding:

  Eventually find the location and position of each element of the first to appear, if i and j are different, the absolute value is, see if you can reach k, if equal to k, explain to meet the meaning of the questions!

1 struct node {
2     int max = -1;
3     int min = -1;
4 };
. 1  BOOL containsNearbyDuplicate_bak (Vector < int > the nums &, int K) {
 2      // two map, a minimum deposit index, a maximum index memory 
. 3      Map < int , int > MP;   // for statistical character appears number 
. 4      Map < int , Node> maxDisMap;   // for storing distances 
. 5      int I, RES = 0 ;
 . 6      for (I = 0 ; I <nums.size (); I ++ )
 . 7      {
 . 8          MP [the nums [I ]] ++ ;
 . 9          IF (MP [the nums [I]] == . 1)
 10          {
 11              // first time, stores the distance 
12 is              maxDisMap [the nums [I]] = min. I;
 13 is          }
 14          the else  IF (MP [the nums [I]]> . 1 ) {
 15              maxDisMap [the nums [I]] = .max I;
 16          }
 . 17      }
 18 is      // difference between the maximum and minimum traverse the array 
. 19      Map < int , Node> :: Iterator IT;
 20 is      for (IT = maxDisMap.begin ();! IT = maxDisMap. End (); I ++ )
 21 am      {
 22 is          IF (it->second.max - it->second.min > res)
23             res = it->second.max - it->second.min;
24     }
25     if (res == k)
26         return true;
27     return false;
28 }

 

  So the question is, for example 2, the first appearance position index 1 is 0, the position index of the last occurrence is 3, the maximum absolute value of the difference is 3, is greater than a given k, Why return true? ? ?

  Discover forum where there are a lot of similar issues in the discussion

  

  So, as long as the presence of two smaller than the absolute value of the difference is equal to k, can be, modify the code as follows:

. 1  BOOL containsNearbyDuplicate (Vector < int > the nums &, int K) {
 2      // two map, a minimum deposit index, a maximum index memory 
. 3      Map < int , int > MP;   // for statistical character appears number 
. 4      Map < int , Node> maxDisMap;   // for storing distances 
. 5      int I, RES = INT_MAX;
 . 6      for (I = 0 ; I <nums.size (); I ++ )
 . 7      {
 . 8          MP [the nums [I ]] ++ ;
 . 9          IF (MP [the nums [I]] == . 1 )
10          {
 11              // first time, the maximum and minimum are kept up 
12 is              maxDisMap [the nums [I]] = min. I;
 13 is              . MaxDisMap [the nums [I]] max = I;
 14          }
 15          the else  IF (MP [the nums [I ]] == 2 ) {
 16              maxDisMap [the nums [I]] max = I;.   // updates the maximum 
. 17              IF .. (maxDisMap [the nums [I]] max - maxDisMap [the nums [I]] min <= K )
 18 is                  return  to true ;
 . 19          }
 20 is          the else {
 21 is             maxDisMap [the nums [I]] = min maxDisMap [the nums [I]] max;..   // pushed back, only two adjacent recording the same value 
22 is              maxDisMap [the nums [I]] = max. I;
 23 is              IF (. maxDisMap [the nums [I]] max - maxDisMap [the nums [I]] min <=. K)
 24                  return  to true ;
 25          }
 26 is      }
 27      return  to false ;
 28 }

  Waiting for a new way to learn ~ ~ ~

 

Guess you like

Origin www.cnblogs.com/cnyulei/p/12021799.html