算法day05

算法day05

242. 有效的字母异位词

  1. 题目链接:https://leetcode.cn/problems/valid-anagram/description/
  2. 第一眼思路:字母异位词:即所含字母相同,字母个数相同,但位置可以不同。所以直接暴力:统计两个字符串的字符个数并记录,最后对比。
  3. 第三四眼思路:第一眼的思路就蛮好的。
  4. 统计字母个数并记录比对的暴力解法:
class Solution {
    public boolean isAnagram(String s, String t) {
        char[] sarr = new char[26];
        char[] tarr = new char[26];
        for(int i = 0;i<s.length();i++){
            char c = s.charAt(i);
            sarr[c-97] += 1;
        }
        for(int i = 0;i<t.length();i++){
            char c = t.charAt(i);
            tarr[c-97] += 1;
        }

        for(int i = 0;i<sarr.length;i++){
            if(sarr[i]!=tarr[i]){
                return false;
            }
        }
        return true;
    }
}
  1. 卡哥的思路比我的写法少了一个数组,直接对一个数组进行操作就好。第一个字符串的字母就++,其他字符串的字母就–

349. 两个数组的交集

  1. 题目链接:https://leetcode.cn/problems/intersection-of-two-arrays/description/
  2. 第一眼思路:所谓两个数组的交集,也就是,A有且B有的元素。用HashSet对A去重,再判断B的元素是否在A中,在A中的话就要放到要输出的数组中。
  3. HashSet代码实现:
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet <Integer> hashSet = new HashSet<>();
        HashSet <Integer> hashSet2 = new HashSet<>();
        for(int i : nums1){
            hashSet.add(i);
        }
        for(int i : nums2){
            hashSet2.add(i);
        }
        int temp = nums1.length>nums2.length?nums1.length:nums2.length;
        int [] arr = new int [temp];
        int len = 0;
        for(int i : hashSet){
            if(hashSet2.contains(i)){
                arr[len++] = i;
            }
        }
        int [] res = new int[len];
        for(int i = 0;i<len;i++){
            res[i] = arr[i];
        }
        return res;
    }
}
  1. 数组方式实现的代码:
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        int [] arr = new int[1001];
        for(int i = 0;i<nums1.length;i++){
            arr[nums1[i]] = 1;
        }
        HashSet<Integer> hashSet = new HashSet<>();
        for(int i : nums2){
            if(arr[i] == 1){
                hashSet.add(i);
            }
        }
        return hashSet.stream().mapToInt(Integer::intValue).toArray();
    }
}

202. 快乐数

  1. 题目链接:
  2. 自己思路:无
  3. 看文章的思路:本质上还是要找到规律:终止条件是当之前的数字再次出现了,表示即将进入死循环,就可以停止了。
  4. 代码
class Solution {
    public boolean isHappy(int n) {
        HashSet <Integer> set = new HashSet<>();
        int nextNum = n;
        set.add(nextNum);
        while(true){
            nextNum = calcNextNum(nextNum);
            if(nextNum==1){
                return true;
            }else if(set.contains(nextNum)){
                return false;
            }else{
                set.add(nextNum);
            }
        }

    }
    public int calcNextNum(int n){
        int res = 0;
        String nstr = ""+n;
        char [] arr = nstr.toCharArray();
        for(int i = 0;i<arr.length;i++){
            int val = Integer.valueOf(arr[i])-48;
            res += val*val;
        }
        //System.out.println("n:"+n+",res:"+res);
        return res;
    }
}

1. 两数之和

  1. 题目链接:https://leetcode.cn/problems/two-sum/description/
  2. 第一眼的想法:两层for循环
  3. 第二眼的想法:无
  4. 暴力代码:
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int [] arr = new int[2];
        for(int i = 0;i<nums.length-1;i++){
            for(int j = i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    arr[0] = i;
                    arr[1] = j;
                    return arr;
                }
            }
        }
        return null;
    }
}
  1. 哈希法,将遍历过的数字都保存下来,如果后面遇到需要的数字去map里找,找到就直接返回下标数组。
  2. 哈希法代码实现:
class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i<nums.length;i++){
            int val = target-nums[i];
            if(map.containsKey(val)){
                int [] arr = new int[2];
                arr[0] = map.get(val);
                arr[1] = i;
                return arr;
            }else{
                map.put(nums[i],i);
            }
        }
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值