Leetcode LeetCode algorithm punch-in learning day 4

5. Numbers that only appear once

Given an array of non-empty integers, except for an element that appears only once, every other element appears twice. Find the element that appears only once.

Explanation:
Your algorithm should have linear time complexity. Can you do it without using extra space?
Insert picture description here
Method 1: Mine, but using sorting, the complexity should not meet the linear requirements

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];
    }
}

Method 2: XOR operation

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Method 3: Hash set

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Six, the intersection of two arrays II

Given two arrays, write a function to calculate their intersection.
Insert picture description here
Method 1: Official, hash table. Use a hash table to store the number of occurrences of each number.

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Method 2: Aiming at the above method, I reproduced and improved it

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);
    }
}

Method 3: Use double pointers after sorting

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);
    }
}

Guess you like

Origin blog.csdn.net/Shadownow/article/details/113181228