哈希表相关题目

 

// class Solution {
//     public boolean isAnagram(String s, String t) {
//         if (s.length() != t.length()) return false;
//         char[] c1 = s.toCharArray();
//         char[] c2 = t.toCharArray();
//         Arrays.sort(c1);
//         Arrays.sort(c2);
//         return Arrays.equals(c1, c2);
//     }
// }


class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < t.length(); i++) {
            char ch = t.charAt(i);
            map.put(ch, map.getOrDefault(ch, 0) - 1);
            if (map.get(ch) < 0) return false;
        }
        return true;
    }
}



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

 

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map = new HashMap<>();
        int n1 = magazine.length();
        for (int i = 0; i < n1; i++) {
            char c = magazine.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        int n2 = ransomNote.length();
        for (int i = 0; i < n2; i++) {
            char c = ransomNote.charAt(i);
            map.put(c, map.getOrDefault(c, 0) - 1);
            if (map.get(c) < 0) return false;
        }
        
        return true;
    }
}

 

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] ch = str.toCharArray();
            Arrays.sort(ch);
            //传char数组可以得到字符串
            String key = new String(ch);
            List<String> list = map.getOrDefault(key, new ArrayList<String>());
            list.add(str);
            map.put(key, list);
        }
        List<List<String>> res = new ArrayList<>();
        //hash表遍历
        for (Map.Entry<String, List<String>> x : map.entrySet()) {
            res.add(x.getValue());
        }
        return res;
        //return new ArrayList<List<String>>(map.values());
    }
}

 


//滑动窗口+数组
class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        int sLen = s.length();
        int pLen = p.length();
        List<Integer> res = new ArrayList<>();
        if (sLen < pLen) return res;
        int[] sCount = new int[26];
        int[] pCount = new int[26];
        for (int i = 0; i < pLen; i++) {
            sCount[s.charAt(i) - 'a']++;
            pCount[p.charAt(i) - 'a']++;
        }
        if (Arrays.equals(sCount, pCount)) {
            res.add(0);
        }
        //这个sLen - pLen 想清楚,避免了数组越界异常
        for (int i = 0; i < sLen - pLen; i++) {
            sCount[s.charAt(i) - 'a']--;
            sCount[s.charAt(i + pLen) - 'a']++;
            if (Arrays.equals(sCount, pCount)) {
                res.add(i + 1);
            }
        }
        return res;
    }
}

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        //用TreeSet和HashSet一样,只不过,TreeSet大小自动排序
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            int num = nums1[i];
            set.add(num);
        }
        //int[] res = new int[nums2.length];
        //List<Integer> list = new ArrayList<>();
        Set<Integer> set1 = new HashSet<>();

        for (int i = 0; i < nums2.length; i++) {
            int num = nums2[i];
            if (set.contains(num)) {
                set1.add(num);
            }
        }

        int[] res = new int[set1.size()];
        int index = 0;
        //int自动类型转换
        for (int x : set1) {
            //执行完后在++
            res[index++] = x; 
        }
        
        return res;
    }
}

 

 

//哈希表
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1.length > nums2.length) {
            return intersect(nums2, nums1);
        }
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int num : nums1) {
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        int[] intersection = new int[nums1.length];
        int index = 0;
        for (int num : nums2) {
            int count = map.getOrDefault(num, 0);
            if (count > 0) {
                intersection[index++] = num;
                count--;
                if (count > 0) {
                    map.put(num, count);
                } else {
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}
//排序+双指针
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int length1 = nums1.length, length2 = nums2.length;
        int[] intersection = new int[Math.min(length1, length2)];
        int index1 = 0, index2 = 0, index = 0;
        while (index1 < length1 && index2 < length2) {
            if (nums1[index1] < nums2[index2]) {
                index1++;
            } else if (nums1[index1] > nums2[index2]) {
                index2++;
            } else {
                intersection[index] = nums1[index1];
                index1++;
                index2++;
                index++;
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}

 

class Solution {
    private int getNext(int n) {
        int sum = 0;
        while (n > 0) {
            int qm = n % 10;
            n = n / 10;
            sum = sum + qm * qm;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getNext(n);
        }
        return n == 1;
    }
}

 

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

    }
}

 


//n个数之和都可以分成两数之和,一个意思
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> count12 = new HashMap<>();
        int n = nums1.length;
        for (int i = 0; i < n; i++) {
            int a = nums1[i];
            for (int j = 0; j < n; j++) {
                int b = nums2[j];
                count12.put((a + b), count12.getOrDefault((a + b), 0) + 1);
            }
        }

        int res = 0;
        for (int i = 0; i < n; i++) {
            int a = nums3[i];
            for (int j = 0; j < n; j++) {
                int b = nums4[j];
                if (count12.containsKey(0 - a - b)) {
                    res = res + count12.get(0 - a - b);
                }
            }
        }
        return res;
    }
}

 

//排序+双指针
//将其中一个数字固定,然后循环查找其他另外两个数字
//会涉及到去重的操作,其中包括固定的数字重复、其他另外两个数字的重复,也是通过循环的方式。
//好的,开始写。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        //首先就是如果,传入的数组是空或者数组的长度小于3,那么直接返回空集合。
        List<List<Integer>> res  = new ArrayList<>();
        //将数组的长度单独写出来,我看基本都是这么操作的,应该会比较方便
        int len = nums.length;
        if (nums == null || len < 3) return res;
        //排序
        Arrays.sort(nums);
        //后面留2个数字才够3个,其实减不减无所谓,不影响多少
        for (int i = 0; i < len - 2; i++) {
            //在这边先判断一下,如果第一个就大于0的话,那么就不存在和为0
            //这里你想了一下,那么最后的数字小于0的话呢,这样是不行的,因为本来就是从第一个数字开始判断
            //只不过是顺带刚好判断了一个第一个数字大于0的情况
            if (nums[0] > 0) return res;
            //指定固定数字的去重,从第二个数字开始
            //用短路与的方式,两侧有一个条件不满足就撤退,嘿嘿
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            //定义指定位置两侧的左右数字
            int left = i + 1;
            int right = len - 1;
            //双指针循环指定while
            while (left < right) {
                int sum = nums[left] + nums[i] + nums[right];
                if (sum == 0) {
                    //将此时的数字加入到list集合
                    res.add(Arrays.asList(nums[left], nums[i], nums[right]));
                    //一般循环之内的循环直接带上外部条件就行。
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--; 
                    //同时左佳佳右渐渐了,因为刚好等于0,去重后,一个不动,一个动肯定不行的
                    left++;
                    right--;
                } else if(sum < 0) {
                    //合小于0,那么就要么加大左边值,要么减小右边值,这里是左
                    left++;
                } else if(sum > 0) {
                    right--;
                }
            }
        }
        return res;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值