LeetCode 242.有效的字母异位词
题目描述
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词
示例
输入: s = "anagram", t = "nagaram"
输出: true
题目链接
https://leetcode.cn/problems/valid-anagram/description/
解题思路
用一个长度为 26的数组记录字符串 s的每个元素出现情况(+1);
再遍历字符串 t,遇到元素在字母表位置时,数组 -1;
如果数组全为 0,则返回 true;否则 false
// 耗时: 3m
class Solution {
    public boolean isAnagram(String s, String t) {
        int[] words = new int[26];
        for (int i = 0; i < s.length(); i++) {
            words[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            words[t.charAt(i) - 'a']--;
        }
        for (int word : words) {
            if (word != 0) {
                return false;
            }
        }
        return true;
    }
}
LeetCode 349.两个数组的交集
题目描述
给定两个数组 nums1 和 nums2 ,返回它们的交集。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
示例
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
题目链接
https://leetcode.cn/problems/intersection-of-two-arrays/description/
解题思路
用 HashSet记录 nums1内的元素,再对 nums2遍历,HashSet中存在则加入结果集中
// 耗时:6m13s
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        List<Integer> ans = new ArrayList<>();
        Set<Integer> hs = new HashSet<>();
        for (int num : nums1) {
            hs.add(num);
        }
        for (int num : nums2) {
            if (hs.contains(num) && !ans.contains(num)) {
                ans.add(num);
            }
        }
        int[] res = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            res[i] = ans.get(i);
        }
        return res;
    }
}
LeetCode 202. 快乐数
题目描述
编写一个算法来判断一个数 n 是不是快乐数。
「快乐数」 定义为:
- 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
 - 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
 - 如果这个过程 结果为 1,那么这个数就是快乐数。
 
如果 n 是 快乐数 就返回 true ;不是,则返回 false 。
示例
输入:n = 19
输出:true
解释:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
题目链接
https://leetcode.cn/problems/happy-number/description/
解题思路
HashSet记录遇到的 n值,如果重复出现,则返回 false
// 5m31s
class Solution {
    public boolean isHappy(int n) {
        Set<Integer> hs = new HashSet<>();
        while (!hs.contains(n)) {
            if (multiSum(n) == 1) {
                return true;
            }
            hs.add(n);
            n = multiSum(n);
        }
        return false;
    }
    public int multiSum(int n) {
        int ans = 0;
        while (n > 0) {
            int a = n % 10;
            ans += a * a;
            n = n / 10;
        }
        return ans;
    }
}
LeetCode 1. 两数之和
题目描述
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
你可以按任意顺序返回答案。
示例
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
题目链接
https://leetcode.cn/problems/two-sum/description/
解题思路
先将数组存入 HashMap中,再遍历数组,查找 HashMap中是否存在 targer - nums[i]的值。
// 4m20s
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hm = new HashMap<>();
        int[] ans = new int[2];
        for (int i = 0; i < nums.length; i++) {
            if (hm.containsKey(target - nums[i])) {
                ans[0] = i;
                ans[1] = hm.get(target - nums[i]);
                break;
            }
            hm.put(nums[i], i);
        }
        return ans;
    }
}
                
评论