1. Title
Contains Duplicate II (Judging if there is a repeating element in the array 2)
2. Address of the topic
https://leetcode.com/problems/contains-duplicate-ii/
3. Topic content
English: Given An array of integers and a integer K, find out whether there is II distinct indices i and j in the array such that nums[i] = Nums[j] and the difference between I and J are at most k.
English: Give an array of integers to determine if there are two element values in the array are the same, and their index value is not greater than K, is true, otherwise returns false
4, a method of tle
If you use the brute force method directly to resolve the run timeout. A section of the Java code for TLE is as follows:
/** * @ function Description: leetcode 219 - contains duplicate ii * @ Developer: tsybius2014 * @ Development Date: October 15, 2015 */public class Solution { /** * See if there are duplicate elements within the array and the adjacent repeating element index interval is less than k * @param nums * @return */ public boolean containsnearbyduplicate (Int[] nums, int k) { if (nums.length <= 1) { return false; } for (int i = 0; i < nums.length; i++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; for (int j = i + 1; j <= i + k && j < nums.length; j++) { if (Nums[i] == nums[j]) { return true; } } } return false; }}
5. Method of solving Problems 1
A more easily thought-out approach is to use HashMap to accomplish the goal, and the way to solve the problem with HashMap is very similar to the way to solve question No. 217 (Contains Duplicate).
The Java code is as follows:
import java.util.hashmap;/** * @ function Description: leetcode 219 - contains duplicate ii * @ Developer: tsybius2014 * @ Development Date: October 15, 2015 */public class solution { /** * See if there are duplicate elements within the array and the adjacent repeating element index interval is less than k * @param nums * @return */ public boolean Containsnearbyduplicate (int[] nums, int k) { if (nums.length <= 1) { return false; } HashMap<Integer, Integer> hashmap = new hashmap<integer, integer> (); for (int i = 0; i < nums.length; i++) { if (Hashmap.containskey (nums[i]) & & i - hashmap.get (Nums[i]) <= k) { return true; } hashmap.put (nums[i], i); } return false; }}
6. Method of solving Problems 2
Another way is to use hashset to solve this problem. HashSet is a collection that is implemented using HASHMAP. In the Add function of HashSet, returns True if the inserted element already exists, otherwise false. The following Java code takes advantage of this nature of HashSet:
import java.util.hashset;/** * @ function Description: leetcode 219 - contains duplicate ii * @ Developer: tsybius2014 * @ Development Date: October 15, 2015 */public class solution { /** * See if there are duplicate elements within the array and the adjacent repeating element index interval is less than k * * @param nums * @return */ public boolean Containsnearbyduplicate (int[] nums, int k) { if (nums.length <= 1) { return false; } HashSet<Integer> hashSet = new HashSet<Integer> (); for (int i = 0; i < nums.length; i++) { if (i > k) { hashset.remove (nums[i - &NBSP;K&NBSP;-&NBSP;1]); } if (!hashset.add (nums[i)) { return true; } } return false; }}
END
Leetcode:contains Duplicate II-Determine if there are duplicate elements in the array 2