leetcode_哈希表相关题目

242. 有效的字母异位词

class Solution {
    public boolean isAnagram(String s, String t) {
        //用数组做哈希表
        int[] count = new int[26];
        for(char c : s.toCharArray())
            count[c-'a']++;
        for(char d : t.toCharArray())
            count[d-'a']--;
        for(int i : count)
        {
            if(i!=0)    return false;
        }
        return true;
    }
}

349. 两个数组的交集

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if(nums1 == null || nums2==null)    return new int[0];
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> ans_set =  new HashSet<>();
        for(int i :nums1)   set1.add(i);
        for(int i:nums2)    
        {
            if(set1.contains(i))
                ans_set.add(i);
        }
        //再将ans由set转化为数组
        int[] ans_arr = new int[ans_set.size()];
        int index=0;
        for(int i : ans_set)
        {
            ans_arr[index++]=i;
        }
        return ans_arr;
    }
}

202. 快乐数

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> ans = new HashSet<>();
        while(n!=1&&!ans.contains(n))//如果已经包含n,说明无限循环 不可能等于1,return false
        {
            ans.add(n);
            n=Nextnumber(n);
        }
        return n==1;
    }

    private int Nextnumber(int n)
    {
        int ans=0;
        while(n>0)
        {
            int temp = n%10;
            ans+=temp*temp;
            n=n/10;
        }
        return ans;
    }
}

1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] ans = new int[2];
        if(nums==null||nums.length==0)  return ans;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++)
        {
            int temp= target-nums[i];
            if(map.containsKey(temp))
            {
                ans[1]=i;
                ans[0]=map.get(temp);
            }
            //键key--数组值 值value--数组下标
            map.put(nums[i],i);
        }
        return ans;
    }
}

454. 四数相加 II

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer,Integer> map = new HashMap<>();
        int count=0;
        int temp;
        for(int i:nums1)
        {
            for(int j:nums2)
            {
                //记录两数之和
                temp= i+j;
                //同时统计出现次数
                if(map.containsKey(temp))
                {
                    map.put(temp,map.get(temp)+1);
                }
                //没出现的前提下,则记录出现次数为1次
                else
                    map.put(temp,1);
            }
        }
        for(int i:nums3)
        {
            for(int j:nums4)
            {
                temp=i+j;
                if(map.containsKey(0-temp))
                    //获取value ,即出现次数
                    count+=map.get(0-temp);
            }
        }
        return count;
    }
}

383. 赎金信

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] count = new int[26];
        for(int i=0;i<magazine.length();i++)
        {
            count[magazine.charAt(i)-'a']++;
        }
        for(int j=0;j<ransomNote.length();j++)
        {
            count[ransomNote.charAt(j)-'a']--;
            if(count[ransomNote.charAt(j)-'a']<0)  return false;
        }
        return true;
    }
}

15. 三数之和

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;i++)
        {
            if(nums[i]>0)   return ans;
            //如果有重复,跳过
            if(i>0&&nums[i-1]==nums[i])  continue;

            int left=i+1,right=nums.length-1;
            while(left<right)
            {
                if(nums[left]+nums[i]+nums[right]>0)
                    right--;
                else if(nums[left]+nums[i]+nums[right]<0)
                    left++;
                else
                {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(nums[left]);
                    temp.add(nums[i]);
                    temp.add(nums[right]);
                    ans.add(temp);
                    while(left<right&&nums[left]==nums[left+1])
                        left++;
                    while(left<right&&nums[right]==nums[right-1])
                        right--;
                    left++;
                    right--;
                }
            }
        }
        return ans;
    }
}

18.四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0; i<nums.length;i++)
        {
            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, right= nums.length-1;
                while(left<right)
                {
                    if(nums[i]+nums[j]+nums[left]+nums[right]>target)
                        right--;
                    else if(nums[i]+nums[j]+nums[left]+nums[right]<target)
                        left++;
                    else
                    {
                        List<Integer> temp =  new ArrayList<>();
                        temp = Arrays.asList(nums[i],nums[j],nums[left],nums[right]);
                        ans.add(temp);
                        while(left<right&&nums[left]==nums[left+1])
                            left++;
                        while(left<right&&nums[right]==nums[right-1])
                            right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return ans;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值