代码随想录 第六天 哈希表理论基础 ● 242.有效的字母异位词 ● 349. 两个数组的交集 ● 202. 快乐数 ● 1. 两数之和

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

思路:既然只判断两个字符串的字母,就一个++,一个- -,最后如果二十六个字母都是零,说明两个字符串相等。

反思:   //charat(i)是返回字符串索引,所以s.charAt(i)-'a'实际上是获取字符串s中第i个字符相对于字母'a'的偏移量。

      增强for: 语法形式是:for (元素类型 元素变量 : 遍历对象) {循环体}。它用于遍历数组、集合或其他可迭代对象。在每次循环迭代时,它会自动获取遍历对象中的下一个元素,并将其赋值给循环中的元素变量。

public boolean isAnagram(String s, String t) {
        int[] record = new int[26];
        for(int i = 0; i<s.length(); i++){
            //charat(i)是返回字符串索引,所以s.charAt(i)-'a'实际上是获取字符串s中第i个字符相对于字母'a'的偏移量。
                record[s.charAt(i)-'a']++;
        }
        for(int i = 0; i<t.length(); i++){
            record[t.charAt(i)-'a']--;
        }
        // record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。等于0就表示是s,t是字母异位数。
        for(int i : record){
            if(i != 0){
                return false;
            }
        }
        return true;
    }

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

思路: 都存到一张哈希表上,第二组数组判断一下哈希表里有没有,有就说明这数是两数组共有的,代码其中的哈希表判断比较重要

    contains()该方法用于检查集合或列表中是否包含指定的元素i。如果包含,则返回true,否则返回false。这种方法通常用于搜索特定元素是否存在于集合或列表中

代码

public int[] intersection(int[] nums1, int[] nums2) {
            Set<Integer> set1 = new HashSet<>();
            Set<Integer> set2 = new HashSet<>();
            for (int i = 0; i < nums1.length; i++) {
                set1.add(nums1[i]);
            }
            for (int i : nums2) {
                if (set1.contains(i)) {
                    set2.add(i);
                }

            }
        //
return resSet.stream().mapToInt(x -> x).toArray();
        //这行代码的作用是将一个ResultSet中的数据映射为一个int类型的数组。具体来说,这行代码首先将ResultSet中的数据转换为一个Stream,然后使用mapToInt()方法将Stream中的元素逐个映射为int类型,最后使用toArray()方法将映射后的结果转换为一个int类型的数组并返回。
            int[] arr = new int[set2.size()];
            int j = 0;
            for (int i : set2) {
                arr[j++] = i;
            }
            return arr;

        }

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

思路:首先是判断最后在不重复的情况下n最后是1,还需要一个累加器,将这些数都放到哈希set里,每次添加之前要确定这个数之前没有存过,并且当 n = 1的时候就说明这个数是快乐数。

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        //确定哈希表里没有出现过,并且n不是1;
        while( n != 1 && !record.contains(n)){
            record.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;

    }
}

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

思路:当题目中需要找到对应下标时用map,这回key,value不同往常,key是元素,value是下标,这样当通过key才能找到下标。

  "containsKey" 是一个用于检查 Map 中是否包含指定键的方法。如果 Map 包含指定键,则返回 true,否则返回 false。

public int[] twoSum(int[] nums, int target) {
            int[] arr = new int[2];
            Map<Integer, Integer> map = new HashMap<>();
            //遍历map中有没有目标值(target - key/temp)
            for (int i = 0; i < nums.length; i++) {
                int temp = target - nums[i];
                if (map.containsKey(temp)) {//判断map里有没有对应的key
                    arr[0] = map.get(temp);
                    arr[1] = i;
            break; }
else//没有就将该组数加到map集合里。 map.put(nums[i], i); } return arr; }

 

posted @ 2024-02-27 15:58  22软工冷薄  阅读(1)  评论(0编辑  收藏  举报