力扣打卡day07

哈希表专项

常用的三种哈希结构
数组(哈希值小且范围可控)
Set(集合)(数值很大)
map(映射)(key对应value时)
242. 有效的字母异位词

class Solution {
    public boolean isAnagram(String s, String t) {
        int[] hash=new int[26];
        for(int i=0;i<s.length();i++){
            hash[s.charAt(i)-'a']++;
        }
        for(int i=0;i<t.length();i++){
            hash[t.charAt(i)-'a']--;
        }
        for(int count:hash){
            if(count!=0){
                return false;
            }
        }
        return true;
    }
}

349. 两个数组的交集

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> result=new HashSet<>();
        HashSet<Integer> hash=new HashSet<>();
         for(int i:nums1){
             hash.add(i);
         }
         for(int i:nums2){
             if(hash.contains(i)){
                 result.add(i);
             }
         }
        //或者可以将下面这段代码写成 return resSet.stream().mapToInt(x -> x).toArray();
        //这段代码主要是将集合转换为数组
         int[] res=new int[result.size()];
         int index=0;
         for(int item:result){
             res[index++]=item;
         }
         return res;
    }
}

第202题. 快乐数

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> hash=new HashSet<>();
        while(n!=1&&!hash.contains(n)){
            hash.add(n);
            n=getNumber(n);
        }
        return n==1;
    }
    public int getNumber(int n){
        int res=0;
        while(n>0){
            int temp=n%10;
            n=n/10;
            res+=temp*temp;
        }
        return res;

    }
}

1.两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res=new int[2];
        Map<Integer,Integer>  hash=new HashMap<>();
        for(int i=0;i<nums.length;i++){
             int temp=target-nums[i];
             if(hash.containsKey(temp)){
                 res[1]=i;
                 res[0]=hash.get(temp);
             }
             hash.put(nums[i],i);
        }
        return res;
    }
}

四数相加 II

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer,Integer> map=new HashMap<>();
        int temp=0;
        int res=0;
        for(int i=0;i<nums1.length;i++){
            for(int j=0;j<nums2.length;j++){
                temp=nums1[i]+nums2[j];
                if(map.containsKey(temp)){
                    map.put(temp,map.get(temp)+1);
                }else{
                    map.put(temp,1);
                }
            }
        }
        for(int i=0;i<nums3.length;i++){
            for(int j=0;j<nums4.length;j++){
                temp=nums3[i]+nums4[j];
                if(map.containsKey(0-temp)){
                    res+=map.get(0-temp);
                }
            }
        }
        return res;
    }
}

15.三数之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res=new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0;i<nums.length;i++){
            //因为数组已经排过序,如果第一个数都大于0那么相加
            //是怎么也不可能等于0的
            if(nums[i]>0){
                return res;
            }
            //nums[i]==nums[i+1]判断的是结果集里面能不能有重复元素
            //对A进行去重操作
            if(i>0&&nums[i]==nums[i-1]){
                continue;
            }
            int left=i+1;
            int right=nums.length-1;
            while(left<right){
                int sum = nums[i] + nums[left] + nums[right];
                if(sum>0){
                    right--;
                }else if(sum<0){
                    left++;
                }else{
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    //left和right去重
                    while(right > left &&nums[left] == nums[left + 1]){
                        left++;
                    }
                    while(right > left &&nums[right] == nums[right - 1]){
                        right--;
                    }
                    right--; 
                    left++;
                }
                
            }
        }
        return res;
    }
}

18.四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
         for (int i = 0; i < nums.length; i++) {

            // 一级剪枝
            if (nums[i] > 0 && nums[i] > target) {
                return result;
            }
            //一级去重
            if (i > 0 && nums[i - 1] == nums[i]) {
                continue;
            }
            for (int j = i + 1; j < nums.length; j++) {
                //二级去重
                if (j > i + 1 && nums[j - 1] == nums[j]) {
                    continue;
                }

 int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        
                        while (right > left && nums[right] == nums[right - 1]) right--;
                        while (right > left && nums[left] == nums[left + 1]) left++;

                        left++;
                        right--;
                    }
                }
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值