[Leetcode] Yuan Chu’s daily selection of questions in detail. Want to find my twin brother nearby?

Title description: 219 duplicate elements exist

Given an integer array and an integer k, judge whether there are two different indexes i and j in the array, such that nums [i] = nums [j], and the absolute value of the difference between i and j is at most k.

Example 1:

输入: nums = [1,2,3,1], k = 3
输出: true

Example 2:

输入: nums = [1,0,1,1], k = 1
输出: true

Example 3:

输入: nums = [1,2,3,1,2,3], k = 2
输出: false

Problem analysis:

The code must meet the following two requirements at the same time

1. Is there the same value

2. Two identical values, the absolute value of the difference between the index values ​​of the two elements does not exceed K.

It is easy to type the code after understanding the title requirements. The following two methods are for your reference

Loop traversal method:

We use two loops to traverse the array. The pointer in the first loop is used to point to the array element, and the pointer in the second loop is used to find the same element in the K-bit range. If an element with the same value is found , It jumps out of the loop and returns true. If it is not found at K bits, the first pointer moves to the next element and continues to search. Until the array is traversed, false is returned if it is still not found.

Animation explanation:

Insert picture description here

Question code:

class Solution {
    
    
    public boolean containsNearbyDuplicate(int[] nums, int k) {
    
    
          //特殊情况
          if(nums.length == 0){
    
    
              return false;
          }
          //遍历数组,代表橙色指针的运行轨迹
          for(int i = 0 ;i<nums.length;i++){
    
    
             int j = i+1;//定义蓝色指针
              //蓝色指针运行轨迹,注意蓝色指针的运行范围
             while(j<nums.length && j<=i+k){
    
    
                 //发现直接输出
                if(nums[i] == nums[j]){
    
    
                     return true;
                }
                 //继续找下一个
                    j++;
              }
           }
         //最后没有发现返回false
          return false;                               
    }
}

HashSet sliding window:

This method is simple, and the idea is also very simple, we use HashSet to save, and then we only let him save K values, after inserting the K+1 value, you need to remove the first value entered.

The core of this idea is that the number of saved values ​​of HashSet is fixed, and all the K elements before the pointer are saved.

Insert picture description here

class Solution {
    
    
    public boolean containsNearbyDuplicate(int[] nums, int k) {
    
    
            //特殊情况
              if(nums.length == 0){
    
    
                  return false;
              }
            //定义hashSet用来存值
              HashSet<Integer> hash = new HashSet<Integer>();
              for(int i = 0;i<nums.length;i++){
    
    
                  //存在直接返回true
                  if(hash.contains(nums[i])){
    
    
                      return true;
                  }
                  //不存在则添加
                  hash.add(nums[i]);
                  //长度超过K
                  if(hash.size()==k+1){
    
    
                      //删除最先出现的值
                      hash.remove(nums[i-k]);
                  }
              }
              return false;
    }
}

If you think the topic is helpful to you, please forward it to the people who need it. If you have time, you can do it yourself. Although it is a simple subject, the pass rate is only 40%. I hope it will be helpful to everyone.
Insert picture description here

Guess you like

Origin blog.csdn.net/tan45du_yuan/article/details/109207059