代码随想录刷题day 6 | 哈希表理论基础 242.有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和

242. 有效的字母异位词 383. 赎金信

class Solution {  //这里只给出了242的代码,赎金信的解法可以说是基本相同的
    public boolean isAnagram(String s, String t) {
        int[] map = new int[26];
        for(char c: s.toCharArray())  map[c-'a']++;
        for(char c: t.toCharArray())  map[c-'a']--;
        for(int i: map)   if(i != 0) return false;
        return true;
    }
}

49. 字母异位词分组

这个题主要用来复习HashMap和ArrayList了,这两个里面的主要接口还是很重要很常用的。

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for(String s : strs){
            String key = getKey(s);
            List arr = map.getOrDefault(key, new ArrayList<String>());
            if(arr.size()==0){
                map.put(key, arr);
            }
            arr.add(s);
        }
        return new ArrayList<List<String>>(map.values());
    }

    public String getKey(String s){
        int[] map = new int[26];
        for(char c: s.toCharArray()){
            map[c-'a']++;
        }
        return Arrays.toString(map);
    }
}

202. 快乐数

Hashmap或者hashset的做法比较简单。今天看到个题解有点意思,使用快慢指针来做,因为一定有循环,所以两者终会相遇,相遇的点是1那就是快乐数,否则不是。

class Solution {  //用hashmap
    public boolean isHappy(int n) {
        HashMap<Integer, Integer> map = new HashMap<>();
        while(n != 1){
            int j = map.getOrDefault(n, 0);
            if(j == 1){return false;}
            map.put(n, 1);
            n = next(n);
        }
        return true;
    }
    public int next(int n){
        int sum = 0;
        while(n != 0){
            int j = n % 10;
            sum += j*j;
            n /= 10;
        }
        return sum;
    }
}
class Solution {  //快慢指针的做法,以后遇到有循环的时候可以考虑使用
    public boolean isHappy(int n) {
        if(n == 1 || next(n) == 1) return true;
        int slow = next(n), fast = next(next(n));
        while(slow != fast){
            slow = next(slow);
            fast = next(next(fast));
        }
        return slow == 1;
    }

    public int next(int n){
        int sum = 0;
        while(n != 0){
            int j = n % 10;
            sum += j*j;
            n /= 10;
        }
        return sum;
    }
}

349. 两个数组的交集

主要学的就是hashset的用法

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for(int i : nums1) set1.add(i);
        int j = 0;
        for(int num : nums2) {
            if(set1.contains(num)) set2.add(num);
        }
        int[] ans = new int[set2.size()];
        for(int num : set2){
            ans[j++] = num;
        }
        return ans;
    }
}

1. 两数之和](https://leetcode.cn/problems/linked-list-cycle-ii/)

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

每天都做点,慢慢也就进步了。不过还是有点畏难情绪,做着做着就偷懒了,慢慢改善吧。

posted @ 2024-07-08 22:24  12点不睡觉还想干啥?  阅读(20)  评论(0)    收藏  举报