哈希表、n数之和问题(双指针、哈希表)
哈希表类型的问题不算复杂,但需要想到哈希表却不容易。
一、有效的字母异位词
s串中每个字母计数++,t串中每个字母计数–,最后看计数数组中是不是所有单词个数都是0,是0才是异位词。
class Solution {
public boolean isAnagram(String s, String t) {
int[] cnt = new int[26];
for (int i = 0; i < s.length(); i++) {
cnt[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
cnt[t.charAt(i) - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] != 0) {
return false;
}
}
return true;
}
}
二、两个数组的交集(简单)
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for(int i:nums1){
set1.add(i);
}
for(int i:nums2){
if(set1.contains(i)){
set2.add(i);
}
}
int[] arr = new int[set2.size()];
int j=0;
for(int i:set2){
arr[j++] = i;
}
return arr;
}
}
三、快乐数(简单)
把之前算过的值存到set中,后续如果在set中查到了,就循环了false,到1了就true。
class Solution {
public boolean isHappy(int n) {
// 无限循环,就是说结果出现在原来的结果中,用set存储之前的结果
HashSet<Integer> set = new HashSet<>();
while (true) {
int temp = 0;
while (n != 0) {
temp += (n % 10) * (n % 10);
n /= 10;
}
n = temp;
if (n == 1) {
break;
}
if (set.contains(n)) {
return false;
} else {
set.add(n);
}
}
return true;
}
}
四、两数之和(简单)
基于当前数去找target - nums[i]。
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
int[] ans = new int[2];
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[] {i, map.get(target - nums[i])};
} else {
map.put(nums[i], i);
}
}
return new int[] {0, 0};
}
}
五、三数之和(中等)
先确定 a + b,再去找0 - (a + b)是否存在。但是存在的问题是,题目中要求,答案不可以包含重复的三元组,如果使用哈希表求解,最后还要去重答案,是很麻烦的。
本题不需要记录下标,只需要记录数字,而上一题需要记录下标,不需要记录下标,那就可以排序,就可以双指针。注意,使用双指针一般是在有序数组的情况下,如果需要记录下标由于排序后下标变化,导致无法使用双指针。
准确的说,本题是三指针,第一个、二个指针固定,第三个指针从最后一个数开始找,同时注意去重!!!
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) {
// 第一个数大于0,不用考虑了
break;
}
// 去重
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
// 第三个数字从数组末尾开始遍历(排序后)
int k = nums.length - 1;
for (int j = i + 1; j < nums.length - 1; j++) {
List<Integer> tmp = new ArrayList<>();
// 再去重
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
// 第三个数的下标一定要大于第二个数的下标,且三数之和大于0,那就必须得移动k
// 把k往小数移动
while (j < k && nums[i] + nums[j] + nums[k] > 0) {
k--;
}
// 不满足,因为必须要选出两个数,重叠的数不可以
if (j >= k) {
break;
}
// 满足题目要求
if (nums[i] + nums[j] + nums[k] == 0) {
tmp.add(nums[i]);
tmp.add(nums[j]);
tmp.add(nums[k]);
ans.add(new ArrayList<>(tmp));
}
}
}
return ans;
}
}
上面代码其实有点复杂,而且双指针的意味并不强烈,可以改成下面的代码形式,更好理解:
class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList();
int len = nums.length;
Arrays.sort(nums); // 排序
for (int i = 0; i < len - 2; i++) {
if(nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
if(i > 0 && nums[i] == nums[i-1]) continue; // 去重
int L = i + 1;
int R = len - 1;
while(L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum == 0){
ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
while (L < R && nums[L] == nums[L + 1]) L++;
while (L < R && nums[R] == nums[R - 1]) R--;
L++;
R--;
}
else if (sum < 0) L++;
else if (sum > 0) R--;
}
}
return ans;
}
}
六、四数之和(中等)
仔细看看,这道题同样可以用上面的方法来做,用双指针,准确的是四指针,前三个指针固定,最后一个指针从最后一个位置开始移动。
这里就直接采用简便的写法即可。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new LinkedList<>();
int n = nums.length;
// 一定要记得排序
Arrays.sort(nums);
for (int i = 0; i < n - 3; i++) {
if (i > 0 && nums[i - 1] == nums[i]) continue;
for (int j = i + 1; j < n - 2; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) continue;
// 最后两个指针做移动双指针(相对)
int k = j + 1;
int t = n - 1;
while (k < t) {
int tmp = nums[i] + nums[j] + nums[k] + nums[t];
if (tmp == target) {
List<Integer> list = new LinkedList<>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[k]);
list.add(nums[t]);
ans.add(list);
// 去重
while (k < t && nums[k] == nums[k + 1]) k++;
while (k < t && nums[t] == nums[t - 1]) t--;
// 寻找下一个答案
k++;
t--;
} else if (tmp > target) {
// 数大了
t--;
} else {
// 数小了
k++;
}
}
}
}
return ans;
}
}
七、四数之和Ⅱ(中等)
注意本题中,四个数组是独立的,而上面的数组都是一个,独立的,那就不需要考虑重复使用的问题。并且题目中只需要求解元组数,直接用哈希表就能解决,不用双指针。
先对nums1、nums2中的两个数求和,存入map中,再去求nums3、nums4中的两个数的和,看它们的和的相反数,在map中是否出现过,出现过几次,方案数就加几次。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
map.put(nums1[i] + nums2[j], map.getOrDefault(nums1[i] + nums2[j], 0) + 1);
}
}
int ans = 0;
for (int i = 0; i < nums3.length; i++) {
for (int j = 0; j < nums4.length; j++) {
if (map.containsKey(-(nums3[i] + nums4[j]))) {
ans += map.get(-(nums3[i] + nums4[j]));
}
}
}
return ans;
}
}