动态算法(基础二)笔记回顾

1、存在重复元素

示例 1:

输入:nums = [1,2,3,1]
输出:true
示例 2:

输入:nums = [1,2,3,4]
输出:false
示例 3:

输入:nums = [1,1,1,3,3,4,3,2,4,2]
输出:true

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);                 //首先先对数组进行排序操作
        int n = nums.length;               //提取出数组长度
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {  //排序之后从小到大,相邻相等则返回true说明有重复
                return true;
            }
        }
        return false;
    }
}

2、只出现一次的数字

示例 1 :

输入:nums = [2,2,1]
输出:1
示例 2 :

输入:nums = [4,1,2,1,2]
输出:4
示例 3 :

输入:nums = [1]
输出:1

class Solution {
    public int singleNumber(int[] nums) {
        if(nums.length==1){                   //如果数组长度为1则返回自身
            return nums[0];
        }else{
            int res=0;                        //定义异或对比值
             for(int i=0;i<nums.length;i++){  //开始逐个遍历
                     res=res^nums[i];         //由于a^0=a,a^a=a的特性,所以有重复值就会归于0,只出现一次则为自身的数值,(这个方法只能用在其他值都是成对出现,且只有一个出现一次的数字)     
             }
             return res;
        }
    }
}

3、两个数组的交集||

示例 1:

输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
示例 2:

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
   if(nums1.length>nums2.length){        //先对比两个数组的长短
       return intersect(nums2,nums1);    //此步操作保证把短的放在nums1,这样可以优化hash
   }
   Map <Integer,Integer> map = new HashMap<Integer,Integer>(); //此步为创建hash表
   for(int num:nums1){                                         //遍历nums1中的数字
       int count = map.getOrDefault(num,0)+1;                  //此步为map集合中有这个key也就是num,无值给默认值0,并且做加一操作
       map.put(num,count);                                      //更新hash表中的键值对信息(也就是num出现了几次)
   }
   int[] intersection = new int[nums1.length];                  //创建新数组(因为是交集所以长不过最短数组长度)
   int index=0;                                                 //新数组的开始位置
   for(int num:nums2){                                          //开始遍历nums2数组
       int count = map.getOrDefault(num,0);                     //找当前数字在 HashMap 中的出现次数
       if(count >0){                                            //大于0说明有交集
           intersection[index++]=num;                           //把数存入到新数组中
           count--;                                             //出现的次数减一
           if(count>0){                                         //还大于0说明出现不止一次
               map.put(num,count);                              //更新减少一次后的键值对信息
           }else{                                               //如果减少后次数为0
               map.remove(num);                                 //则把这个数值从hash表中移除
                }
            }
        }
        return Arrays.copyOfRange(intersection,0,index);         // 返回交集数组(截取前 index 部分,因为 intersection 数组可能会有多余部分)
    }
}

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值