摘要:輸入一個整數(shù)數(shù)組,查看數(shù)組中是否存在重復(fù)的值。新的數(shù)組中數(shù)組的下標(biāo)為原數(shù)組的值,如果遍歷過,則設(shè)置為。這里使用了作為實現(xiàn)的數(shù)據(jù)結(jié)構(gòu),通過堆的形式對集合中的數(shù)據(jù)進(jìn)行存儲,從而我們可以通過某種順序獲得該集合中的所有順序。
217 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
輸入一個整數(shù)數(shù)組,查看數(shù)組中是否存在重復(fù)的值。
思路一:hashmap or hashset使用java中的數(shù)據(jù)結(jié)構(gòu)將已經(jīng)遍歷起來的值存儲起來,然后查詢當(dāng)前的值是否已經(jīng)便利過
public boolean containsDuplicate(int[] nums) { Map思路二:min-max重映射map = new HashMap (); for(int curNum : nums){ if(map.containsKey(curNum)) return true; else map.put(curNum, 1); } return false; }
獲得該整數(shù)數(shù)組的最大值和最小值,并且利用最大值和最小值將原數(shù)組映射到新的數(shù)組。新的數(shù)組中數(shù)組的下標(biāo)為原數(shù)組的值,如果遍歷過,則設(shè)置為1。
public boolean containsDuplicate2(int[] nums){ if(nums==null || nums.length<=1) return false; int length = nums.length; int min = nums[0]; int max = nums[0]; for(int curNum : nums){ if(curNum < min) min = curNum; if(curNum > max) max = curNum; } //如果數(shù)組中最大值和最小值之間的數(shù)字個數(shù)小于數(shù)組長度,則一定存在重復(fù)值 if(max - min + 1< length) return true; int[] result = new int[max-min+1]; for(int curNum : nums){ int newIndex = curNum - min; if(result[newIndex] != 0) return true; else result[newIndex]++; } return false; }219 Contains DuplicateII
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.
同I,還是整數(shù)數(shù)組,不同的是,要求兩個重復(fù)值的之間的間隔不得超過k
思路一:hashmap還是通過hashmap存儲已經(jīng)遍歷過的選項以及最近一次遍歷到時的下標(biāo)值。如果重復(fù)數(shù)值的下標(biāo)之間的值不超過k,那么就證明重復(fù)值滿足條件
public boolean containsNearbyDuplicate(int[] nums, int k) { MapindexMap = new HashMap (); for(int i = 0 ; i 思路二:set 保留一個含有最近k個值的集合,如果這個集合中存在和當(dāng)前值相同的值,那么就存在相同的值,無需再去去判斷相同值之間的下標(biāo)是否符合要求。
public boolean containsNearbyDuplicate2(int[] nums, int k){ Set220 Contains DuplicateIIIpotentialSet = new HashSet (); for(int index = 0 ; index < nums.length ; index++){ int curNum = nums[index]; if(potentialSet.contains(curNum)){ return true; } potentialSet.add(curNum); if(potentialSet.size() > k) potentialSet.remove(nums[index-k]); } return false; } Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.這題在II的基礎(chǔ)上重新定義了相同的條件,也就是如果兩個值之間的絕對差不超過t,那么就可以稱這兩個值相同。
這里使用了treeset作為實現(xiàn)的數(shù)據(jù)結(jié)構(gòu),treeset通過堆的形式對集合中的數(shù)據(jù)進(jìn)行存儲,從而我們可以通過某種順序獲得該集合中的所有順序。public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { if(nums == null || nums.length <= 1 || k == 0) return false; TreeSetpotentialNums = new TreeSet (); for(int i = 0 ; i = curNum) return true; potentialNums.add(curNum); if(i >= k) potentialNums.remove((long)nums[i-k]); } return false; }
想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/70759.html
摘要:代碼集合法復(fù)雜度時間空間思路同樣使用集合,但這次我們要維護(hù)集合的大小不超過,相當(dāng)于是記錄一個寬度為的窗口中出現(xiàn)過的數(shù)字。 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. Your function should return true if any v...
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false ...
摘要:題目鏈接題目分析返回給定的數(shù)組中是否有元素重復(fù)出現(xiàn)。思路用和即可最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 D90 217. Contains Duplicate 題目鏈接 217. Contains Duplicate 題目分析 返回給定的數(shù)組中是否有元素重復(fù)出現(xiàn)。 思路 用count和array_unique即可 最終代碼
摘要:題目詳情輸入一個整數(shù)的數(shù)組,如果數(shù)組中的元素有重復(fù)的,那么返回,如果數(shù)組中的元素都是唯一的,那么返回思路這道題理解起來比較簡單,首先還是要注意一下邊界條件異常輸入,對于長度小于等于的數(shù)組做一個直接的返回對于這種要考慮數(shù)組中元素的重復(fù)的問題, 題目詳情 Given an array of integers, find if the array contains any duplicate...
Problem 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 ...
閱讀 1602·2021-09-02 15:41
閱讀 1003·2021-09-02 15:11
閱讀 1288·2021-07-28 00:15
閱讀 2313·2019-08-30 15:55
閱讀 1149·2019-08-30 15:54
閱讀 1698·2019-08-30 15:54
閱讀 2981·2019-08-30 14:02
閱讀 2530·2019-08-29 16:57