算法修炼Day06|242.有效的字母异位词 ● 349. 两个数组的交集 ● 202. 快乐数 ● 1. 两数之和

70 阅读2分钟

哈希系列:

当遇到判断元素是否存在集合中的时候,就可以考虑使用哈希法。

题目:242. 有效的字母异位词 - 力扣(LeetCode)

代码实现:

方法一:先判断长度是否相等,不相等返回false;相同继续下面逻辑,分别进行遍历执行++ -- 操作,map方式注意删除key。 方法二:小写字母可以使用字符数组。

// map实现,注意当数量为1时删除Key
class Solution {
    public boolean isAnagram(String s, String t) {
        // 分别遍历,做++ -- 操作
        Map<Character, Integer> map = new HashMap<>();
        if (s.length() != t.length()) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }
        for (int i = 0; i < t.length(); i++) {
            if (map.get(t.charAt(i)) == null) {
                return false;
            } else if (map.get(t.charAt(i)) == 1) {
                map.remove(t.charAt(i));
            } else if (map.get(t.charAt(i)) > 1) {
                map.put(t.charAt(i), map.get(t.charAt(i)) - 1);
            }
        }
        if (map.size() > 0) {
            return false;
        }
        return true;
    }
}
// 26个英文字符,分别对应97...,当应用在数组下标时,需要注意做 -97 的操作以保证数组下标在[0,25]。
class Solution {
    public boolean isAnagram(String s, String t) {
        int[] ans = new int[26];
        if (s.length() != t.length()) return false;
        for (int i = 0; i < s.length(); i++) {
            ans[s.charAt(i) - 'a']++; // 'a'可以换成97
            ans[t.charAt(i) - 'a']--; // 同上
        }
        for (int num : ans) {
            if (num != 0) {
                return false;
            }
        }
        return true;
    }
}

题目:349. 两个数组的交集 - 力扣(LeetCode)

方法一:题目中的用例需要去重,首选set。

方法二:方法一优化,两个set,第一个去重,第二个存相同值。遍历第二个set存入结果集中。

代码实现:

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                if (nums1[i] == nums2[j]) {
                    if (set.contains(nums1[i])) break;
                    set.add(nums1[i]);
                }
            }
        }
        int[] ans = new int[set.size()];
        int index = 0;
        for (int num : set) {
            ans[index++] = num;
        }
        return ans;
    }
}

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        // 两个set,第一个去重,第二个存相同值
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            if (set1.contains(nums1[i])) {
                continue;
            } else {
                set1.add(nums1[i]);
            }
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set1.contains(nums2[i]) && !set2.contains(nums2[i])) {
                set2.add(nums2[i]);
            }
        }
        int[] ans = new int[set2.size()];
        int index = 0;
        for (int num : set2) {
            ans[index++] = num;
        }
        return ans;
    }
}

题目:202. 快乐数 - 力扣(LeetCode)

方法:一个set实现避免无限循环的去重,递归实现有限循环。

代码实现:

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

题目:1. 两数之和 - 力扣(LeetCode)

方法一:双层for循环暴力实现;

方法二:一层for循环,从map中获取值,存在则输出结果,一遍遍历结束时结束。

代码实现:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] ans = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    ans[0] = i;
                    ans[1] = j;
                }
            }
        }
        return ans;
    }
}

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