力扣LeetCode算法打卡学习第4天

五、只出现一次的数字

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
在这里插入图片描述
方法1:我的,但是使用了排序的话,复杂度应该就不符合线性的要求了

class Solution {
    public int singleNumber(int[] nums) {
        if(nums.length<2){
            return nums[0];
        }
        Arrays.sort(nums);
        if(nums[0]!=nums[1]){
            return nums[0];
        }
        for(int i=1;i<nums.length-1;i++){
            if(nums[i]!=nums[i-1] && nums[i]!=nums[i+1]){
                return nums[i];
            }
        }
        return nums[nums.length-1];
    }
}

方法2:异或操作

class Solution {
    public int singleNumber(int[] nums) {
        int single = 0;
        for (int num : nums) {
            single = single ^ num;
        }
        return single;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/single-number/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

方法3:哈希集

public static int SingleNumber(int[] nums)
{
    HashSet<int> set = new HashSet<int>();
    for (int i = 0; i < nums.Length; i++)
    {
        //该判断语句的整体作用是:如果当前数字(nums[i])已经在之前出现过,那么在哈希集实例(set)中移除当前数字
        // Add 方法的作用是添加当前数字于哈希集中,如果当前数字和该集合(set)元素存在重复,则返回 False 。故在此采用了逻辑非操作符(!)
        if (!set.Add(nums[i]))
            set.Remove(nums[i]); ;
    }
    //因为每个重复元素最多存在两个,而重复元素的第一个添加后均被移除,而第二个均未添加成功,故此时哈希集只保留唯一且未重复的元素
    // First*1 方法的作用是返回该序列的第一个元素
    return set.First();
}

作者:magicalchao
链接:https://leetcode-cn.com/problems/single-number/solution/cou-yi-pian-ti-jie-hua-shuo-ti-jie-hen-hao-wan-by-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

六、两个数组的交集 II

给定两个数组,编写一个函数来计算它们的交集。
在这里插入图片描述
方法1:官方,哈希表。用哈希表存储每个数字出现的次数。

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        //把小的数组放前面
        if (nums1.length > nums2.length) {
            return intersect(nums2, nums1);
        }
        //创建一个哈希表
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        //如果表中有了,就把值取出来加1;如果表中没有,就0加1
        for (int num : nums1) {
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        int[] intersection = new int[nums1.length];
        int index = 0;
        //把num2中的值一一取出来和哈希表中比较
        for (int num : nums2) {
            int count = map.getOrDefault(num, 0);
            //如果count>0,则代表哈希表中有这个数
            if (count > 0) {
                intersection[index++] = num;
                count--;
                if (count > 0) {
                    map.put(num, count);
                } else {
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/solution/liang-ge-shu-zu-de-jiao-ji-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

方法2:针对以上方法,我复现并改进了一下

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if(nums1.length>nums2.length){
            return intersect(nums2,nums1);
        }
        Map<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int num:nums1){
            int count=map.getOrDefault(num, 0) + 1;
            map.put(num,count);
        }
        int[] willback = new int[nums1.length];
        int index=0;
        for(int num:nums2){
            int count = map.getOrDefault(num, 0);
            if(count>0){
                willback[index]=num;
                index++;
                map.put(num,count-1);
            }
        }
        return Arrays.copyOfRange(willback, 0, index);
    }
}

方法3:排序后用双指针

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int length1=nums1.length;
        int length2=nums2.length;
        int[] willback = new int[Math.min(length1,length2)];
        int index1=0;
        int index2=0;
        int index3=0;
        while (index1 < length1 && index2 < length2) {
            if(nums1[index1]<nums2[index2]){
                index1++;
            }
            else if(nums1[index1]>nums2[index2]){
                index2++;
            }
            else{
                willback[index3]=nums1[index1];
                index3++;
                index1++;
                index2++;
            }
        }
        return Arrays.copyOfRange(willback,0,index3);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值