Day5 哈希表,242有效的字母异位词,349两个数组的交集,202 快乐数 1两数之和

文章介绍了如何在Java中使用哈希表(hashtable)实现字符串是否为变位词的判断,以及如何使用数组模拟哈希表在两个整数数组中查找和的功能。同时提到了`Happy`类用于判断数字序列是否为快乐数的问题。
摘要由CSDN通过智能技术生成

hashtable 哈希表的作用在一快速判断一个元素是否在表里面,利用哈希函时把传入数字化

class Solution {
    public boolean isAnagram(String s, String t) {
        // 最开始想的是最为char元素放入set中,但是set无序不能重复,所以就换map,记录s中
        // 字母出现的次数,用t出现减去,之后迭代判断map中value是否为零,但是map迭代taimaf
        // 所以最后选择利用数组模拟hashtable,只有小写字母所以数组长度26就可以了

        // 这是map集合方法,效率太低了
        // char[] sChar = s.toCharArray();
        // char[] tChar = t.toCharArray();
        // Map<Character,Integer> hashTable = new HashMap<Character,Integer>();

        // // 先判断长度是否相等
        // if(s.length() != t.length()) return false;

        // // s加入map 并且自增
        // for(int i = 0; i < sChar.length; i++){

        //     // int sum = 0;
        //     // if(hashTable.containsKey(sChar[i]))
        //     //     sum = hashTable.get(sChar[i]);

        //     // hashTable.put(sChar[i] , sum == 0 ? 1 : sum+1);
        //     // 开始忘记了getordefault方法,上面重写了一个
        //     hashTable.put(sChar[i] , hashTable.getOrDefault(sChar[i],0) + 1);
        //     // int ss = hashTable.getOrDefault(hashTable.get(sChar[i]),0) + 1;
        // }

        // // t 加入hasntable 进行自减
        // for(int i = 0; i < tChar.length; i++){
        //     if(!hashTable.containsKey(tChar[i])){
        //         // return false;
        //     }else{
        //         int num =  hashTable.get(tChar[i]);
        //         if(num <= 0) return false;
        //         hashTable.put(tChar[i],num -1);
        //     }
        // }

        // //
        // for(Character key : hashTable.keySet()){
        //     if(hashTable.get(key) == 0) continue;
        //     else return false;
        // }
        // return true;

        // 数组构建hashtable方法
        int[] tempArr = new int[26];
        char[] sChar = s.toCharArray();
        char[] tChar = t.toCharArray();

        for(int i = 0; i < sChar.length; i++){
            int num = tempArr[sChar[i] - 'a'] ;
            tempArr[sChar[i] - 'a'] = num + 1;
        }

        for(int i = 0; i < tChar.length; i++){
            int num = tempArr[tChar[i] - 'a'] ;
            tempArr[tChar[i] - 'a'] = num - 1;
        }

        for(int i = 0; i < tempArr.length; i++){
            if(tempArr[i] != 0) return false;
        }
        return true;

    }
}

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        // set 就可以直接做,数组模拟hashtable也可以 map也可以 选择map

        HashSet<Integer> set = new HashSet<>();
        Map<Integer,Integer> resMap = new HashMap<>();
        for(int i = 0; i < nums1.length; i++){
            set.add(nums1[i]);
        }

        for(int i = 0; i < nums2.length; i++){
            if(set.contains(nums2[i])){
                resMap.put(nums2[i],resMap.getOrDefault(nums2[i],0));
            }
        }

        int[] res = new int[resMap.size()];
        int count = 0;
        for (Integer i :
                resMap.keySet()) {
            res[count] = i;
            count++;
        }
        return res;
    }
}

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

    private int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }
}

class Solution {
   public int[] twoSum(int[] nums, int target) {
        // 暴力
        // int[] res = new int[2];
        // for(int one = 0;one < nums.length-1 ;one++){
        //     for(int two = one + 1; two < nums.length; two++){
        //         int temp = nums[one];
        //         int tar = target - temp;

        //         if(nums[two] == tar){
        //             res[0] = one;
        //             res[1] = two;
        //             return res;
        //         }

        //     }
            

        // }
        // return res;

        int[] res = new int[2];
        if(nums == null || nums.length == 0){
            return res;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            int temp = target - nums[i];   // 遍历当前元素,并在map中寻找是否有匹配的key
            if(map.containsKey(temp)){
                res[1] = i;
                res[0] = map.get(temp);
                break;
             }
            map.put(nums[i], i);    // 如果没找到匹配对,就把访问过的元素和下标加入到map中
        }
        return res;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值