[053] LeetCode 219演算法【Contains Duplicate II】 包含重複值 II

M
Leetcode 演算法教學
Jan 4, 2020

219. Contains Duplicate II (Easy)

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

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

這道題目跟前一題的唯一差別在於找到重複的數值以後,要在判斷他們是不是距離在k之間,這題直接用Map去紀錄就好,hash或set自己選。

大家加油。

上一篇:[052] LeetCode 217演算法【Contains Duplicate】 包含重複值

下一篇:[054] LeetCode 219演算法【Contains Duplicate III】 包含重複值 III

--

--