代码随想录算法训练营第六天| 242.有效的字母异位词, 349. 两个数组的交集, 202. 快乐数, 1. 两数之和

242. Valid Anagram

  • 思路
    • Map
      • key为字母
      • value为出现次数
    • 先遍历一遍s, 加Map
    • 再遍历一遍t, check一下,如果有value
  • java
    class Solution {
        public boolean isAnagram(String s, String t) {
            if (s.length() != t.length()){
                return false;
            }
            Map map = new HashMap<>();
            for (int i = 0; i < s.length(); i++){
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1 );
            }
            
            for (char c : t.toCharArray()){
                if(!map.containsKey(c)){
                    return false;
                }
                map.put(c, map.get(c) - 1);
                if (map.get(c) < 0){
                    return false;
                }
            }
            return true;
        }
    }
    

 349. Intersection of Two Arrays

  • 思路
    • 两个set
      • set1记录nums1 集合
      • set2记录nums2与set1之间的重复数
      • 输出set2
    • java
      class Solution {
          public int[] intersection(int[] nums1, int[] nums2) {
              Set set1 = new HashSet<>();
              Set set2 = new HashSet<>();
              for(int i : nums1){
                  set1.add(i);
              }
              for(int i : nums2){
                  if(set1.contains(i)){
                      set2.add(i);
                  }
              }
              int index = 0;
              int[] res = new int[set2.size()];
              for(int i : set2){
                  res[index++] = i;
              }
              return res;
          }
      }

202. Happy Number

  • 思路
    • Set去重
    • 主要是套了一个各个digit的平方和的壳子
    • java
      //这个是自己做的版本,helper输出的各个digit组成的list,可以进一步输出平方和
      class Solution {
          public boolean isHappy(int n) {
              Set set = new HashSet<>();
              
              while( !set.contains(n) ){
                  set.add(n);
                  ArrayList list = helper(n);
                  int sum = 0;
                  for(int i : list){
                      sum += i*i;
                  }
                  if(sum == 1){
                      return true;
                  }
                  n = sum;
              }
              return false;
          }
          
          private ArrayList helper(int n) {
              ArrayList list= new ArrayList<>();
              while ( n != 0){
                  int digit = n % 10;
                  list.add(digit);
                  n /= 10;
                  
              }
              return list;
          }
      }

 1. Two Sum

  • HashMap
    • java
      //创建HashMap
      HashMap map = new HashMap();
      //遍历
      for(int i = 0; i < nums.length; i++){
        //如果找到两个数,则return
        if(map.containsKey(target - nums[i])){
          return new int []{i, map.get(target-nums[i])};
        }
        //找不到,则放到map里
        map.put(nums[i], i);
      }
      return null;
      
      //Time Complexity On
      //Space Complexity On
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值