Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間3年前 (2022/10/21 09:12), 編輯推噓1(101)
留言2則, 2人參與, 3年前最新討論串57/719 (看更多)
219. Contains Duplicate II 給予一個整數陣列,若該整數陣列存在重複值,且它們距離不超過k,返回true反之返 回false。 Input: nums = [1,2,3,1], k = 3 Output: true 思路: 1.用HashMap儲存上次訪問元素nums[i]的索引,若HashMap存在nums[i]則檢查i - Map(nums[i]) 是否小於等於k,是就返回true。 2.都沒有符合條件就返回false。 JavaCode: class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++) { if(map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) return true; map.put(nums[i], i); } return false; } } 姆咪 -- https://i.imgur.com/fHpKflu.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.231.19.35 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1666314778.A.884.html

10/21 09:14, 3年前 , 1F
大師
10/21 09:14, 1F

10/21 22:56, 3年前 , 2F
大師
10/21 22:56, 2F
文章代碼(AID): #1ZKV8QY4 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZKV8QY4 (Marginalman)