03 哈希表

242. 有效的字母异位词

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

public boolean isAnagram2(String s, String t) {
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        Arrays.sort(sChars);
        Arrays.sort(tChars);
        return Arrays.equals(sChars, tChars);
    }

public boolean isAnagram3(String s, String t) {
        Map<Character,Integer> map=new HashMap<>();
        char[] sChars=s.toCharArray();
        char[] tChars=t.toCharArray();
        for(char ch:sChars){
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        for(char ch:tChars){
            Integer count=map.get(ch);
            if(count==null){
                return false;
            }else if(count>1){
                map.put(ch,count-1);
            }else{
                map.remove(ch);
            }
        }
        return map.isEmpty();
    }

 349. 两个数组的交集

public int[] intersection1(int[] nums1, int[] nums2) {
        Set<Integer> resSet=new HashSet<>();
        Set<Integer> set=new HashSet<>();
        for(int i:nums1){
            set.add(i);
        }
        for(int j:nums2){
            if(set.contains(j)){
                resSet.add(j);
            }
        }
        int[] res=new int[resSet.size()];
        int i=0;
        for(int j:resSet){
            res[i]=j;
            i++;
        }
        return res;
    }

//双指针
public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> resSet=new HashSet<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i=0,j=0;
        while(i<nums1.length&&j<nums2.length){
            if(nums1[i]==nums2[j]){
                resSet.add(nums1[i]);
                i++;
                j++;
            }else if(nums1[i]>nums2[j]){
                j++;
            }else if(nums1[i]<nums2[j]){
                i++;
            }
        }
        int[] res=new int[resSet.size()];
        i=0;
        for(int z:resSet){
            res[i]=z;
            i++;
        }
        return res;
    }

350. 两个数组的交集 II

202. 快乐数

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

1. 两数之和

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值