20230915刷题记录
参考【代码随想录】来刷的。
关键词:哈希表、双指针
思考一下
- 什么时候使用哈希表?
- 哈希表用什么实现?数组、set、还是map?
- 使用双指针可降低时间复杂度。
- 剪枝可以降低执行用时。
1 两数之和
梦开始的地方,1.两数之和
力扣题目链接
很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
如果想要一个for循环写完的话,则需要在遍历到nums[i]的时候,判断之前有没有遍历过 target - nums[i]。
而要实现这种效果,很明显的想法是使用哈希表,判断一个元素是否在哈希表中的时间复杂度是O(1)。
现在需要考虑的是究竟是使用数组实现还是使用 set 来实现,因为使用数组实现可以节省时间。
如果想使用数组实现的话,就必须知道 nums[i] 的最大值,因为要把 nums[i] 作为数组的索引。而且 nums[i] 的分布不能太稀疏,因为这样会浪费大量内存。比如 nums = [1, 1000, 10000],就不适合用数组来实现哈希表。
故这道题应该用 set 来实现。
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i]; // 遍历当前元素,并在map中寻找是否有匹配的key
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
break;
}
map.put(nums[i], i); // 如果没找到匹配对,就把访问过的元素和下标加入到map中
}
return res;
}
2 四数相加 II
454.四数相加 II
力扣题目链接
分组,nums1和nums2一组,nums3和nums4一组。
判断是否存在m, n,使得
n
u
m
s
3
[
i
]
+
n
u
m
s
4
[
j
]
=
n
u
m
s
1
[
m
]
+
n
u
m
s
2
[
n
]
.
nums3[i] + nums4[j] = nums1[m] + nums2[n].
nums3[i]+nums4[j]=nums1[m]+nums2[n].
学习一下map.getOrDefault(key, defaultValue)
,存在key时,返回key对应的value;不存在时,返回defaultValue。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int res = 0;
for(int n1: nums1)
for (int n2: nums2) {
map.put(n1 + n2, map.getOrDefault(n1 + n2, 0) + 1);
}
for(int n3: nums3)
for (int n4: nums4) {
res += map.getOrDefault(-(n3 + n4), 0);
}
return res;
}
}
3 赎金信
383.赎金信
力扣题目链接
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
ransom 能由 magazines 里面的字符构成的条件为:
ransom 里面字符的出现次数要小于 magazines 里面的相同字符的出现次数。
思路就很明显了,使用哈希表存下 ransom 中每个字符的出现次数,遍历 magazines,使当前字符的对应的value减1。
这里的哈希表显然可以用数组来实现,因为 ransom 和 magazine 都是由小写字母组成。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] map = new int[26];
for (int i = 0; i < ransomNote.length(); i++) {
++map[ransomNote.charAt(i) - 'a'];
}
for (int i = 0; i < magazine.length(); i++) {
--map[magazine.charAt(i) - 'a'];
}
for (int i: map)
if (i > 0)
return false;
return true;
}
}
4 三数之和
- 三数之和
力扣题目链接
剑指Offer专项突破版上也有这道题。
排序+双指针
为什么要使用排序+双指针
因为在一个有序的数组中,使用双指针找出和为target的两个数的时间复杂度为O(n),空间复杂度为O(1)。
令left = 0, right = nums.length - 1。
当nums[left] + nums[right] < target
时,left向右移动;
当nums[left] + nums[right] > target
时,right向左移动;
直到nums[left] + nums[right] > target
或 left == right
为止
剪枝
可有效降低运行时间,如图所示,减少了10ms。
// 剪枝
if (nums[i] > 0)
return;
解题代码
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums.length < 3)
return null;
List result = new ArrayList<List<Integer>>();
Arrays.sort(nums);
int i = 0;
while (i < nums.length - 2) {
twoSum(nums, i, result);
// 去重
// 三元组不能再以nums[i]开头了
int temp = nums[i];
while (temp == nums[i] && i < nums.length - 2) {
++i;
}
}
return result;
}
private void twoSum(int[] nums, int i, List<List<Integer>> result) {
// 剪枝
if (nums[i] > 0)
return;
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[i] + nums[left] + nums[right] < 0) {
++left;
} else if (nums[i] + nums[left] + nums[right] > 0) {
--right;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
// 去重
// 以nums[i]开头的三元组,不能再使用nums[left]了
int temp = nums[left];
while (temp == nums[left] && left < right)
++left;
}
}
}
}
5 四数之和
- 四数之和
力扣题目链接
和三数之和是一个思路。
时间复杂度为O(n^3)
也要使用剪枝
// 剪枝
if (nums[i] + nums[j] > target && nums[j] >= 0)
return;
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
if (nums.length < 4)
return new ArrayList<List<Integer>>();
Arrays.sort(nums);
List result = new ArrayList<List<Integer>>();
int i = 0;
while (i < nums.length - 3) {
int j = i + 1;
while (j < nums.length - 2) {
twoSum(nums, i, j, target, result);
int temp = nums[j];
while (temp == nums[j] && j < nums.length - 2)
++j;
}
int temp = nums[i];
while (temp == nums[i] && i < nums.length - 3)
++i;
}
return result;
}
private void twoSum(int[] nums, int i, int j, int target, List<List<Integer>> result) {
// 剪枝
if (nums[i] + nums[j] > target && nums[j] >= 0)
return;
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
if ((long) nums[i] + nums[j] + nums[left] + nums[right] < target)
++left;
else if ((long) nums[i] + nums[j] + nums[left] + nums[right] > target)
--right;
else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
int temp = nums[left];
while (temp == nums[left] && left < right)
++left;
}
}
}
}
6 总结
参考代码随想录
一般来说哈希表都是用来快速判断一个元素是否出现集合里。
对于哈希表,要知道哈希函数和哈希碰撞在哈希表中的作用。
哈希函数是把传入的key映射到符号表的索引上。
哈希碰撞处理有多个key映射到相同索引上时的情景,处理碰撞的普遍方式是拉链法和线性探测法。
接下来是常见的三种哈希结构:
- 数组
- set(集合)
- map(映射)
一些同学可能想,用数组干啥,都用 map 不就完事了。
用 map 确实可以,但使用map的空间消耗要比数组大一些,因为 map 要维护红黑树或者符号表,而且还要做哈希函数的运算。所以数组更加简单直接有效!