算法训练营day05|● 哈希表理论基础 ● 242.有效的字母异位词 ● 349. 两个数组的交集 ● 202. 快乐数● 1. 两数之和

242.有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例 1: 输入: s = "anagram", t = "nagaram" 输出: true

示例 2: 输入: s = "rat", t = "car" 输出: false

说明: 你可以假设字符串只包含小写字母。

package Demo0311;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: 阿斋
 * @Date Created in 2024/3/11 && 18:33
 * @Description:242.有效的字母异位词
 */
public class LeetCode242 {
//    //方法一
//    @Test
//    public void test(){
//        String s = "anagram" , t = "nagaram";
//        System.out.println(isAnagram(s,t));
//    }
//
//    private boolean isAnagram(String s, String t) {
//        if(s.length() != t.length()){
//            return false;
//        }
//        //1把数据放到list集合
//        //2排序
//        //3对比
//        List<Character> s1 = new ArrayList<>();
//        List<Character> t1 = new ArrayList<>();
//        for (int i = 0; i < s.length(); i++) {
//            s1.add(s.charAt(i));
//            t1.add(t.charAt(i));
//        }
//        Collections.sort(s1);
//        Collections.sort(t1);
//        return s1.toString().equals(t1.toString());

//    }

    //方法二
    public boolean isAnagram(String s, String t){
        int[] record = new int[26];
        for (int i = 0; i < s.length(); i++) {
            record[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            record[t.charAt(i) - 'a']--;
        }
        for(int count:record){
            if(count != 0){
                return false;
            }
        }
        return true;
    }
}

 

349. 两个数组的交集

题意:给定两个数组,编写一个函数来计算它们的交集。

方法1:将resSet集合转换为整数数组并返回。这里使用了Java 8中的Stream API,将resSet中的元素映射为整数,然后转换为整数数组返回

方法2:另外申请一个整数数组arr,大小为resSet的大小,然后遍历resSet集合,将元素添加到arr数组中。最后返回arr数组

package Demo0311;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: 阿斋
 * @Date Created in 2024/3/11 && 20:36
 * @Description:349. 两个数组的交集
 *版本一:使用HashSet
 * class Solution {
 *     public int[] intersection(int[] nums1, int[] nums2) {
 *         if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
 *             return new int[0];
 *         }
 *         Set<Integer> set1 = new HashSet<>();
 *         Set<Integer> resSet = new HashSet<>();
 *         //遍历数组1
 *         for (int i : nums1) {
 *             set1.add(i);
 *         }
 *         //遍历数组2的过程中判断哈希表中是否存在该元素
 *         for (int i : nums2) {
 *             if (set1.contains(i)) {
 *                 resSet.add(i);
 *             }
 *         }
 *
 *         //方法1:将结果集合转为数组
 *
 *         return resSet.stream().mapToInt(x -> x).toArray();
 *
 *         //方法2:另外申请一个数组存放setRes中的元素,最后返回数组
 *         int[] arr = new int[resSet.size()];
 *         int j = 0;
 *         for(int i : resSet){
 *             arr[j++] = i;
 *         }
 *
 *         return arr;
 *     }
 * }
 * 版本二:使用Hash數組
 * class Solution {
 *     public int[] intersection(int[] nums1, int[] nums2) {
 *         int[] hash1 = new int[1002];
 *         int[] hash2 = new int[1002];
 *         for(int i : nums1)
 *             hash1[i]++;
 *         for(int i : nums2)
 *             hash2[i]++;
 *         List<Integer> resList = new ArrayList<>();
 *         for(int i = 0; i < 1002; i++)
 *             if(hash1[i] > 0 && hash2[i] > 0)
 *                 resList.add(i);
 *         int index = 0;
 *         int res[] = new int[resList.size()];
 *         for(int i : resList)
 *             res[index++] = i;
 *         return res;
 *     }
 * }
 */
public class LeetCode349 {
//    public int[] intersection(int[] nums1, int[] nums2){
//        if((nums1 == null && nums1.length == 0) || (nums2 == null && nums2.length == 0)){
//            return new int[0];
//        }
//        Set<Integer> set1 = new HashSet<>();
//        Set<Integer> resSet = new HashSet<>();
//
//        for(int i: nums1){
//            set1.add(i);
//        }
//
//        for(int i:nums2){
//            if(set1.contains(i)){
//                resSet.add(i);
//            }
//        }
//
        return resSet.stream().mapToInt(x -> x).toArray();
//        int[] arr =new int[resSet.size()];
//        int j = 0;
//        for(int i : resSet){
//            arr[j++] = i;
//        }
//        return arr;
//
//    }
    public int[] intersection(int[] nums1,int[] nums2){
        int[] hash1 = new int[1002];
        int[] hash2 = new int[1002];
        for(int i : nums1){
            hash1[i]++;
        }
        for(int i : nums2){
            hash2[i]++;
        }
        List<Integer> resList = new ArrayList<>();
        for (int i = 0; i < 1002; i++) {
            if(hash1[i] > 0 && hash2[i] > 0){
                resList.add(i);
            }
        }
        int index = 0;
        int res[] = new int[resList.size()];
        for(int i : resList){
            res[index] = i;
            index++;
        }
        return res;
    }

}

第202题. 快乐数

编写一个算法来判断一个数 n 是不是快乐数。

「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为  1,那么这个数就是快乐数。

如果 n 是快乐数就返回 True ;不是,则返回 False 。

示例:

输入:19
输出:true
解释:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1

package Demo0311;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: 阿斋
 * @Date Created in 2024/3/11 && 21:31
 * @Description:第202题. 快乐数
 * 编写一个算法来判断一个数 n 是不是快乐数。
 *
 * 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为  1,那么这个数就是快乐数。
 *
 * 如果 n 是快乐数就返回 True ;不是,则返回 False 。
 *
 * 示例:
 *
 * 输入:19
 * 输出:true
 * 解释:
 * 1^2 + 9^2 = 82
 * 8^2 + 2^2 = 68
 * 6^2 + 8^2 = 100
 * 1^2 + 0^2 + 0^2 = 1
 * 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;
 *     }
 */
public class LeetCode202 {
    public boolean isHap(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;
    }
}

1. 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]

package Demo0311;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 阿斋
 * @Date Created in 2024/3/11 && 22:36
 * @Description:1两数之和
 */
public class LeetCode1 {
    public int[] twoSum(int[] nums, int target) {
        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];
            if(map.containsKey(temp)){
                res[i] = i;
                res[0] = map.get(temp);
                break;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}

 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值