# 哈希映射用法及算法例题

## 例题

### 1、两数之和

#### 解题思路及实现

HashMap入门的经典题。

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
int[] result = new int[2];
for (int i = 0; i < nums.length; i++) {
int value = nums[i];
int find = target - value;
if (map.containsKey(find) && map.get(find) != i) {
result[0] = i;
result[1] = map.get(find);
break;
}
map.put(nums[i], i);
}
return result;
}
}


### 2、同构字符串

#### 解题思路及实现

class Solution {
public boolean isIsomorphic(String s, String t) {
char[] char1 = s.toCharArray();
char[] char2 = t.toCharArray();
HashMap<Character, Integer> map1 = new HashMap<>();
HashMap<Character, Integer> map2 = new HashMap<>();
for (int i = 0; i < char1.length; i++) {
char c1 = char1[i];
char c2 = char2[i];
if (!map1.containsKey(c1))
map1.put(c1, i);
if (!map2.containsKey(c2))
map2.put(c2, i);

if (map1.get(c1) != map2.get(c2))
return false;
}
return true;
}
}


### 3、两个列表的最小索引总和

#### 题目描述

https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/

#### 解题思路及实现

class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
HashMap<String, Integer> map = new HashMap<>();
List<Integer> posList = new ArrayList<>();
int minPosNum = Integer.MAX_VALUE;

for (int i = 0; i < list1.length; i++) {
map.put(list1[i], i);
}
for (int i = 0; i < list2.length; i++) {
String favorite = list2[i];
Integer position = map.get(favorite);
if (position != null) {
int posNum = i + position;
if (minPosNum > posNum) {
minPosNum = posNum;
posList.clear();
} else if (minPosNum == posNum) {
}
}
}
String[] result = new String[posList.size()];
for (int i = 0; i < posList.size(); i++)
result[i] = list2[posList.get(i)];
return result;
}
}


### 4、字符串中的第一个唯一字符

#### 题目描述

s = “leetcode”

s = “loveleetcode”

#### 解题思路及实现

class Solution {
public int firstUniqChar(String s) {
char[] arr = s.toCharArray();
HashMap<Character, Integer> map = new HashMap<>();
// 遍历将所有字符的频率输出到Map中
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (map.get(c) == 1)
return i;
}
return -1;
}
}


### 5、两个数组的交集 II

#### 解题思路及实现

class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();

// 遍历数组将所有元素和其数量存入map中
for (int i = 0; i < nums1.length; i++) {
int key = nums1[i];
if (map.containsKey(key)) {
map.put(key, map.get(key) + 1);
} else {
map.put(key, 1);
}
}

int[] result = new int[nums2.length];
int pos = 0;
for (int i = 0; i < nums2.length; i++) {
int key = nums2[i];
if (map.containsKey(key) && map.get(key) >= 1) {
result[pos] = key;
pos++;
map.put(key, map.get(key) - 1);
}
}

return Arrays.copyOf(result, pos);
}
}


### 6、存在重复元素 II

#### 解题思路及实现

class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int key = nums[i];
if (map.containsKey(key)) {
int oldPos = map.get(key);
if (i - oldPos <= k) {
return true;
} else {
map.put(key, i);
}
} else {
map.put(key, i);
}
}
return false;
}
}


## 参考 & 感谢

• https://leetcode-cn.com/explore/learn/card/hash-table/205/practical-application-hash-map/809/

• https://leetcode-cn.com/problems/two-sum/
• https://leetcode-cn.com/problems/isomorphic-strings/
• https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/
• https://leetcode-cn.com/problems/first-unique-character-in-a-string/
• https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/
• https://leetcode-cn.com/problems/contains-duplicate-ii/

## 关于我

Hello，我是 却把清梅嗅 ，如果您觉得文章对您有价值，欢迎 ❤️，也欢迎关注我的 博客 或者 GitHub

11-17 2577

10-21 6648

12-05 2280

03-23 5万+

07-30 9万+

01-20 4万+

01-17 4612

04-25 7万+

03-09 542

06-02 4万+

01-10 4307

04-06 257

06-26 1万+

01-20 1009

05-07 196

04-09 1460

02-13 571

04-29 125

09-21 1万+

06-30 1969

05-07 282

04-09 2560

01-07

05-07 78

04-30 3万+

03-09 391

01-19 7602

08-07 2844

04-09 2万+

02-23 2928

05-02 90

12-19 7666

04-22 66

03-10 1万+

04-14 112

04-21 190

03-23 2万+

05-16 7563

03-18 592

01-20 4万+

03-30 17万+

04-06 7万+

06-13 189

03-08 2万+

04-04 4076

05-07 128

03-22 5万+

03-19 4万+

03-16 10万+

03-19 6万+

03-10 2万+

03-26 207

02-11 1万+

03-27 5万+

10-09 889

04-17 5万+

03-15 7万+

03-10 19万+

03-23 4万+

05-07 95

05-07 3万+

05-06 4万+

03-13 11万+

05-08 5万+

02-17 8824

04-07 5万+

01-20 3万+

03-30 4万+

03-12 11万+

01-02 242

06-25 171

04-11 618

04-23 103

04-15 527

05-14 235

#### 【20190514】【每天一道算法题】设计哈希映射（哈希表）

©️2019 CSDN 皮肤主题: 成长之路 设计师: Amelia_0503