文章目录
核心:一般哈希表都是用来快速判断一个元素是否出现集合里。
考法:如 连续子数组问题,结合滑动窗口, 前缀和思想去考
242.有效的字母异位词
public boolean isAnagram(String s, String t) {
//数组就是简单的哈希表,但是数组的大小可不是无限开辟的
int[] res = new int[26];
for(char c : s.toCharArray()){
res[c-'a'] += 1;
}
for(char c : t.toCharArray()){
res[c-'a'] -= 1;
}
for(int i : res){
if(i!=0) return false;
}
return true;
}
1002. 查找常用字符
349. 两个数组的交集
202. 快乐数
1. 两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
int[] res = new int[2];
for(int i=0;i<nums.length;i++){
if(map.containsKey(target-nums[i])){
res[0]=map.get(target-nums[i]);
res[1]=i;
return res;
}
map.put(nums[i],i);
}
return res;
}
}
拓展
15.三数之和
双指针,注意去重
public List<List<Integer>> threeSum(int[] nums) {
//排序原数组,Arrays.sort()快速排序,
//遍历该有序数组,每遍历到一个位置,利用左右两端的双指针,
//if三数之和<0,左指针往中间移动;>0,右指针往中间移动
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i=0;i<nums.length;i++){
if(nums[i] > 0){
return res;
}
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
int left=i+1,right=nums.length-1;
while(left < right){
int sum = nums[i] + nums[left] + nums[right];
if(sum == 0){
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
//去重 while!!!
while(right>left && nums[left] == nums[left+1]) left++;
while(right>left && nums[right] == nums[right-1]) right--;
left++;
right--;
}else if(sum > 0){
right--;
}else{
left++;
}
}
}
return res;
}
18.四数之和
双指针
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
for(int j=i+1;j<nums.length;j++){
if(j > i+1 && nums[j] == nums[j-1]){//
continue;
}
int left=j+1,right=nums.length-1;
while(left < right){
int tmp = nums[i]+nums[j]+nums[left]+nums[right];
if(tmp == target){
res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
//去重
while(left < right && nums[left] == nums[left+1]) left++;
while(left < right && nums[right] == nums[right-1]) right--;
left++;
right--;
}else if(tmp > target){
right--;
}else{
left++;
}
}
}
}
return res;
}
454.四数相加II
哈希表
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map = new HashMap<>();
int res = 0;
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
int tmp = nums1[i]+nums2[j];
map.put(tmp,map.getOrDefault(tmp,0)+1);
}
}
for(int i=0;i<nums3.length;i++){
for(int j=0;j<nums4.length;j++){
int tmp = 0-(nums3[i]+nums4[j]);
if(map.containsKey(tmp)){
res += map.get(tmp);
}
}
}
return res;
}
383. 赎金信
136.只出现一次的数字
public int singleNumber(int[] nums) {
// int ans = nums[0];
// if (nums.length > 1) {
// for (int i = 1; i < nums.length; i++) {
// ans = ans ^ nums[i];
// }
// }
// return ans;
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++){
map.put(nums[i],map.getOrDefault(nums[i],0)+1);
}
for(Integer num : map.keySet()){
if(map.get(num) == 1) return num;
}
return -1;
}
138.复制带随机指针的链表
public Node copyRandomList(Node head) {
if(head==null) {
return null;
}
//创建一个哈希表,key是原节点,value是新节点
Map<Node,Node> map = new HashMap<Node,Node>();
Node p = head;
//将原节点和新节点放入哈希表中
while(p!=null) {
Node newNode = new Node(p.val);
map.put(p,newNode);
p = p.next;
}
p = head;
//遍历原链表,设置新节点的next和random
while(p!=null) {
Node newNode = map.get(p);
//p是原节点,map.get(p)是对应的新节点,p.next是原节点的下一个
//map.get(p.next)是原节点下一个对应的新节点
if(p.next!=null) {
newNode.next = map.get(p.next);
}
//p.random是原节点随机指向
//map.get(p.random)是原节点随机指向 对应的新节点
if(p.random!=null) {
newNode.random = map.get(p.random);
}
p = p.next;
}
//返回头结点,即原节点对应的value(新节点)
return map.get(head);
}
560.和为k的子数组(前缀和)
//前缀和
// public class Solution {
// public int subarraySum(int[] nums, int k) {
// int len = nums.length;
// // 计算前缀和数组
// int[] preSum = new int[len + 1];
// preSum[0] = 0;
// for (int i = 0; i < len; i++) {
// preSum[i + 1] = preSum[i] + nums[i];
// }
// int count = 0;
// for (int left = 0; left < len; left++) {
// for (int right = left; right < len; right++) {
// // 区间和 [left..right],注意下标偏移
// if (preSum[right + 1] - preSum[left] == k) {
// count++;
// }
// }
// }
// return count;
// }
// }
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int subarraySum(int[] nums, int k) {
// key:前缀和,value:key 对应的前缀和的个数
Map<Integer, Integer> preSumFreq = new HashMap<>();
// 对于下标为 0 的元素,前缀和为 0,个数为 1
preSumFreq.put(0, 1);
int preSum = 0;
int count = 0;
for (int num : nums) {
preSum += num;
// 先获得前缀和为 preSum - k 的个数,加到计数变量里
if (preSumFreq.containsKey(preSum - k)) {
count += preSumFreq.get(preSum - k);
}
// 然后维护 preSumFreq 的定义
preSumFreq.put(preSum, preSumFreq.getOrDefault(preSum, 0) + 1);
}
return count;
}
}
525.连续数组(前缀和)
前缀和的运用
将问题转化为:如何求得最长一段区间和为 0 的子数组。
class Solution {
public int findMaxLength(int[] nums) {
int n = nums.length;
int[] sum = new int[n + 1];
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (nums[i - 1] == 1 ? 1 : -1);
int ans = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 2; i <= n; i++) {
if (!map.containsKey(sum[i - 2])) map.put(sum[i - 2], i - 2);
if (map.containsKey(sum[i])) ans = Math.max(ans, i - map.get(sum[i]));
}
return ans;
}
}