Algorithm topic:
The following sources: https: //leetcode-cn.com/problems/contains-duplicate-ii
Given an array of integers and k an integer, whether there are two different indices i and j is determined array, such nums [i] = nums [j], and the maximum absolute value of the difference is k i and j.
Example 1:
Input: the nums = [1,2,3,1], K =. 3
to true: Output
Example 2:
Input: the nums = [1,0,1,1], K =. 1
to true: Output
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
python3 Code:
class Solution: def containsNearbyDuplicate(self, nums, k): #先判断是否存在重复元素 nums_len=len(nums) set_len=len(set(nums)) if set_len == nums_len: return False elif set_len < nums_len: for i in range(nums_len): for j in range(i+1,nums_len): # for j in range(nums_len-i): if nums[i] == nums[j]: if abs(i-j)<=k: # print(i ,j) return True return False
Summarize ideas and knowledge
1.2 for determining layer for loop start value
for i in range(nums_len):
for j in range(i+1,nums_len):
2. Under what circumstances returns False
3. code coverage as more test cases and the array length is zero for the case where the 1 [] [1]
4.set tuple string of digital dictionaries list the commonly used method to master several data structures