leetcode force buckle - Sword finger offer brush question - day3-004 A number that appears only once

Posted by BryonS on Mon, 24 Jan 2022 03:44:53 +0100

Sword finger Offer II 004 Number that appears only once - LeetCode (LeetCode CN. Com)https://leetcode-cn.com/problems/WGki4K/

You want to use brute force cracking through array traversal directly, but in the use case [43,16,45,89,45, - 2147483648,452147483646, - 2147483647, - 2147483648,432147483647, - 2147483646, - 2147483648,89, - 2147483646, - 2147483646, - 2147483646, 72147483646, - 2147483647,16,162147483646,43]

In this case, the array access will be out of bounds.

class Solution {
    public int singleNumber(int[] nums) {
        int result=-1;
        //findMax
        int max=nums[0];
        int min=nums[0];
        for(int i=1;i<nums.length;i++){
            if(max<nums[i]){
                max=nums[i];
            }
            if(min>nums[i]){
                min=nums[i];
            }
        }
        if(min<0){
            for(int i=0;i<nums.length;i++){
                nums[i]-=min;
            }
            max-=min;
        }
        //initialization
        int []array=new int[max+1];
        for(int i=0;i<array.length;i++){
            array[i]=-1;
        }
        for(int i=0;i<nums.length;i++){
            array[nums[i]]+=1;
        }
        for(int i=0;i<array.length;i++){
            if(array[i]==0){
                result=i;
            }
        }
        if(min<0){
            result+=min;
        }
        return result;
    }
}

Reflection: the master of bit operation is poor, and the verification of boundary value is always ignored.

Change your mind and use the topic conditions. The same number must appear three times.

class Solution {
    public int singleNumber(int[] nums) {
        int result=-1;
        Arrays.sort(nums);
        for(int i=0;i<nums.length;i++){
            System.out.println(nums[i]);
        }
        if(nums.length==1){
            result=nums[0];
        }else{
            for(int i=0;i<nums.length-1;i+=3){
                if(nums[i]!=nums[i+1]){
                    result=nums[i];
                    System.out.println("result="+nums[i]);
                    break;
                }
            }
            if(nums[nums.length-1]!=nums[nums.length-2]){
               result=nums[nums.length-1];
            }
        }
        
        return result;
    }
}

 

Numb, the complexity is too poor.

Kangkang official problem solving.

Method 1: hash table
Ideas and algorithms

We can use hash mapping to count the number of occurrences of each element in the array. For each key value pair in the hash map, the key represents an element and the value represents its number of occurrences.

After the statistics are completed, we can traverse the hash map to find the elements that appear only once.

class Solution {
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> freq = new HashMap<Integer, Integer>();
        for (int num : nums) {
            freq.put(num, freq.getOrDefault(num, 0) + 1);
        }
        int ans = 0;
        for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
            int num = entry.getKey(), occ = entry.getValue();
            if (occ == 1) {
                ans = num;
                break;
            }
        }
        return ans;
    }
}

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/WGki4K/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-0vrt/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Complexity analysis

Time complexity: O(n), where nn is the length of the array.

Space complexity: O(n). The hash map contains at most ⌊ n/3 ⌋ + 1 elements, that is, the required space is O(n).

Method 2: determine each binary bit in turn
Ideas and algorithms

For the convenience of narration, we call "elements that appear only once" as "answers".

Since the elements in the array are within the range of \ texttt{int}int (i.e. 3232 bit integer), we can calculate whether each binary bit of the answer is 00 or 11 in turn.

Specifically, consider the second binary bit of the answer (ii is numbered from 00), which may be 00 or 11. For the non answer elements in the array, each element appears 33 times, corresponding to 33 00 or 33 11 of the second binary bit. In either case, their sum is a multiple of 33 (that is, the sum is 00 or 33). Therefore:

The second bit of the answer is the remainder of the sum of the second bit of all elements in the array divided by 33.

In this way, for each element xx in the array, we use the bit operation \ texttt {(x > > i) \ & 1} (x > > i) & 1 to get the second binary bit of xx, add them, and then remainder 33. The result must be 00 or 11, that is, the second binary bit of the answer.

details

It should be noted that if the language used does not distinguish between "signed integer type" and "unsigned integer type", you may get the wrong answer. This is because the 3131st binary bit (i.e., the highest bit) of "signed integer type" (i.e., type \ texttt{int}int) is a sign bit in the sense of complement, corresponding to - 2 ^ {31} − 2
31
The "unsigned integer type" has no sign, and the 3131st binary bit corresponds to 2 ^ {31} 2
31
 . Therefore, in some languages (such as \ texttt{Python}Python), special judgment needs to be made on the highest bit.

class Solution {
    public int singleNumber(int[] nums) {
        int ans = 0;
        for (int i = 0; i < 32; ++i) {
            int total = 0;
            for (int num: nums) {
                total += ((num >> i) & 1);
            }
            if (total % 3 != 0) {
                ans |= (1 << i);
            }
        }
        return ans;
    }
}

Author: LeetCode-Solution
 Link: https://leetcode-cn.com/problems/WGki4K/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-0vrt/
Source: force buckle( LeetCode)
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Complexity analysis

Time complexity: O(nlogC), where nn is the length of the array and CC is the data range of the element. In this question, \ log C=\log 2^{32} = 32logC=log2
32
= 32, that is, we need to traverse the 0\sim310 ∼ 31st binary bit.

Space complexity: O(1).

Author: leetcode solution
Link: https://leetcode-cn.com/problems/WGki4K/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-0vrt/
Source: LeetCode
 

 

Topics: Java Algorithm leetcode