?
目錄
1、HashMap的基本方法
1.1、基礎方法(增刪改查)
1.2、其他方法?
2、HashMap的相關例題
2.1、題目介紹
2.2、解題
2.2.1、解題思路
2.2.2、解題圖解
2.3、解題代碼
1、HashMap的基本方法
HashMap 是一個散列表,它存儲的內容是鍵值(key-value)映射。
HashMap 的 key 與 value 類型可以相同也可以不同,根據(jù)定義,不受限制。
1.1、基礎方法(增刪改查)
1.定義一個哈希表
HashMap<Integer, String> hashmap= new HashMap<Integer, String>();
2.添加鍵值對(key-value)(增)
hashmap.put(1, "string1"); // 執(zhí)行完后hash表內為{1=string1}
hashmap.put(2, "string2"); // 執(zhí)行完后hash表內為{1=string1, 2=string2}
hashmap.put(2, "string2"); // 執(zhí)行完后hash表內為{1=string1, 2=string2, 3=string3}
3.根據(jù)key值訪問value(查)
hashmap.get(1); // 返回string1
hashmap.get(2); // 返回string2
hashmap.get(3); // 返回string3
4.根據(jù)key值刪除元素(刪)
hashmap.remove(1); // 執(zhí)行完后hash表內為{2=string2, 3=string3}
hashmap.remove(2); // 執(zhí)行完后hash表內為{3=string3}
hashmap.remove(3); // 執(zhí)行完后hash表內為{}
// 刪除所有鍵值對
hashmap.clear();
5.替換 hashMap 中是指定的key對應的 value(改)
hashmap.replace(key,value); // 返回0
6.返回hashmap中鍵值對的數(shù)量
hashmap.size(); // 返回0
7.getOrDefault(Object key, V defaultValue)
此方法用于當Map集合中有這個key時,就使用這個key對應的value值,如果沒有就使用默認值defaultValue;hashmap.getOrDefault(key,defaultValue);
1.2、其他方法?
1.檢查hashMap中是否存在指定的key對應的映射關系
hashmap.containsKey(key);?
2.檢查hashMap中是否存在指定的value對應的映射關系
hashmap.containsValue(value);?
3.hashmap是否為空
hashmap.isEmpty();?
4.HashMap.values() 方法
hashmap.values(); // 返回所有Value值組成的集合
例如:
?如果有HashMap: {1=Google, 2=Runoob, 3=Taobao}
?則返回Values: [Google, Runoob, Taobao]
2、HashMap的相關例題
2.1、題目介紹
原題鏈接:128. 最長連續(xù)序列 - 力扣(LeetCode)
?
示例 1:
輸入:nums = [100,4,200,1,3,2] 輸出:4 解釋:最長數(shù)字連續(xù)序列是[1, 2, 3, 4]。它的長度為 4。
示例 2:
輸入:nums = [0,3,7,2,5,8,4,6,0,1] 輸出:9
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
2.2、解題
2.2.1、解題思路
使用一個HashMap存儲當前遍歷過的數(shù)字,如果hashMap中已經存在這個數(shù)字,說明之前已經處理過這個數(shù)字,不做任何處理【是為了防止出現(xiàn)重復數(shù)字再次計算造成錯誤】
每次遍歷到新數(shù)字時,去hashMap中尋找比它小1的數(shù)字和比它大1的數(shù)字對應的長度,記為min和max。
如果min、max均為0,說明此時不存在上下界,直接記為1.
當出現(xiàn)min、max不為0時,說明與當前數(shù)字有新的上下界,計算出上下界所對應的數(shù)字,并在map中修改對應的上下界大小。?
若不存在上下界:直接更新為1;
若存在上界不存在下界:更新上界數(shù)字長度+1,即max + 1;
若存在下界不存在上界:更新下界數(shù)字長度+1,即min + 1;
若上下界均存在:同時更新上下界長度為下界長度+上界長度+1,即min + max + 1
2.2.2、解題圖解
數(shù)組nums從i=0開始遍歷,has為哈希表,result用來保存最后的結果,min用來保存鍵值(key)為 nums[?i-1 ] 在哈希表中所對應的值(value)?;max用來保存鍵值(key)為 nums[?i+1 ] 在哈希表中所對應的值(value) ,now保存當前循環(huán)最長連續(xù)序列的結果用于和result進行比較?
?
當前的 i = 0 ,nums[ i ] = 100,???has 中沒有 key 為 100 的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中沒有 key 為 99(nums[i]-1) 的項, 所以?min = 0 ;由于 has 中沒有 key 為 101(nums[i]+1) 的項, 所以 max = 0 ;因此 now = 1 ;然后在 has 中添加 key = 100,value = 1 的項;result 小于 now,所以讓 result = now = 1
當前的 i = 1?,nums[ i ] = 4,???has 中沒有 key 為 4?的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中沒有 key 為 3(nums[i]-1) 的項, 所以?min = 0 ;由于 has 中沒有 key 為 5(nums[i]+1) 的項, 所以 max = 0 ;因此 now = 1 ;然后在 has 中添加 key = 4,value = 1 的項;result 等于?now,所以?result 不變
當前的 i = 2?,nums[ i ] = 200,???has 中沒有 key 為 200 的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中沒有 key 為 199(nums[i]-1) 的項, 所以?min = 0 ;由于 has 中沒有 key 為 201(nums[i]+1) 的項, 所以 max = 0 ;因此 now = 1 ;然后在 has 中添加 key = 200,value = 1 的項;result 等于?now,所以?result 不變
當前的 i = 3?,nums[ i ] = 1,???has 中沒有 key 為 1?的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中沒有 key 為 0(nums[i]-1) 的項, 所以?min = 0 ;由于 has 中沒有 key 為 2(nums[i]+1) 的項, 所以 max = 0 ;因此 now = 1 ;然后在 has 中添加 key = 1,value = 1 的項;result 等于?now,所以?result 不變
當前的 i = 4?,nums[ i ] = 3,???has 中沒有 key 為 3?的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中沒有 key 為 2(nums[i]-1) 的項, 所以?min = 0 ;由于 has 中有 key 為 4(nums[i]+1) 的項, 所以 max = 1?;因此 now = 2?;然后在 has 中添加 key = 3,value = 2?的項,添加 key = 3 + 1, value = 2 的項(has.put(nums[i]+max, now));result 小于 now,所以讓 result = now = 2
當前的 i = 5?,nums[ i ] = 2,???has 中沒有 key 為 2?的項,所以讓 min = has.getOrDefault(nums[i]-1, 0);max = has.getOrDefault(nums[i]+1, 0);由于 has 中有 key 為 1(nums[i]-1) 的項, 所以?min = 1?;由于 has 中有 key 為 3(nums[i]+1) 的項, 所以 max = 2?;因此 now = 4?;然后在 has 中添加 key = 2,value = 4?的項,添加?key = 2?+ 1, value = 4?的項(has.put(nums[i]+max, now)),添加 key = 2?-?1, value = 4?的項(has.put(nums[i]-min, now));result 小于 now,所以讓 result = now = 4
?
最后返回 result?,result 等于 4
2.3、解題代碼
class Solution {
public int longestConsecutive(int[] nums) {
HashMap<Integer, Integer> has = new HashMap<>();
int result = 0;
for (int i=0; i<nums.length; i++){
if (has.get(nums[i]) != null){
continue;
}
int min = has.getOrDefault(nums[i]-1, 0);
int max = has.getOrDefault(nums[i]+1, 0);
int now = min + max + 1;
if (min == 0 && max == 0){
has.put(nums[i], now);
} else if (min == 0){
has.put(nums[i]+max, now);
has.put(nums[i], now);
} else if (max == 0){
has.put(nums[i], now);
has.put(nums[i]-min, now);
} else{
has.put(nums[i]+max, now);
has.put(nums[i], 1);
has.put(nums[i]-min, now);
}
result = Math.max(result,now);
}
return result;
}
}
?
時間復雜度: O(n)
空間復雜度: O(n)
?
【LeetCode力扣】相關:
【LeetCode力扣】42.接雨水(困難)-CSDN博客https://blog.csdn.net/m0_65277261/article/details/134291521?spm=1001.2014.3001.5502【LeetCode力扣】287.尋找重復數(shù)(中等)-CSDN博客https://blog.csdn.net/m0_65277261/article/details/134232926?spm=1001.2014.3001.5502【LeetCode力扣】11. 盛最多水的容器 (中等)-CSDN博客https://blog.csdn.net/m0_65277261/article/details/134102596?spm=1001.2014.3001.5502文章來源:http://www.zghlxwxcb.cn/news/detail-753764.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-753764.html
到了這里,關于java數(shù)據(jù)結構(哈希表—HashMap)含LeetCode例題講解的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!