Leetcode- Single Number II

本文介绍了一种算法,用于在给定数组中找出仅出现特定次数的唯一元素,适用于数组中元素出现次数从一次到任意次数的情况。通过位操作简化了复杂度,提供了通用解决方案,并展示了易于理解的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given an array of integers, every element appears three times except for one. Find that single one.

Note: We generalize this problem to:

Given an array of integers, every element appears k times except for one which apprears l times. Find that single one whic appears l times.

Analysis:

The general solution is given in https://oj.leetcode.com/discuss/857/constant-space-solution

Now I try to explain the general solution.

First, we only consider one bit (either one in the bit sequence of each element in A). The idea is to record how many times 1 is appreaed for this bit. We use an array X with length k to indicate 0 - k-1 times. If 1 appreas j times, then X[j] is marked as 1. We scan the array A, and want to express such logic:

1. If A[i] on this bit is 0, then X array remains its original value.

2. If A[i] on this bit is 1, then

  2.1 if 1 has appreared j-1 times, i.e., X[j-1]==1, then X[j] =1 now.

  2.2 if 1 has appreared j times, i.e., X[j]==1, then X[j]=0 now.

  2.3 if 1 has appreaed some other times, then X[j] remains the same, i.e., still 0.

We use logic table to express this logic:

 

X[j-1]X[j]A[i]new X[j]
0101
1000
0000
110N/A
0 110
1011
0010
111N/A

Note that X[j-1]==1 && X[j]==1 will not happen.

Then we change the table to logic expression formula:

new X[j] = (~X[j-1] & X[j] & ~A[i]) | (X[j-1] & ~X[j] & A[i]).

Since X[j] & X[j-1] will not happen, it does not matter what output it will be, so we assume the output will be 1 to simply the expression, then we have:

X[j] = (X[j] & ~A[i]) | (X[j-1] & A[i])

NOTE: This is the formula used in the general solution, but we have not reached there. CONTINUE.

Since if 1 apprears k times, then it equals to apprear 0 times, we have X[0] = (old X[k-1] & A[i]) | (X[0] & ~A[i]).

Now, with this formula, we can count how many times the 1 has appreared on some specific bit position for array A. It also means that we can know that what the value that apprears l times on some specific bit position. It is the value of X[l]. NOTE, it is either 1 or 0.

Second, since there is no relationship between each bit in a number, we can directly use the above formula to operate the number, the answer then will be the value that apprears exactly l times on each bit, which is the ANSWER we want.

Solution:

NOTE: This solution is posted by Ronmocy in LeetCode.

 1 public class Solution {
 2     public int singleNumber(int[] A, int k, int l) {
 3         if (A == null) return 0;
 4         int t;
 5         int[] x = new int[k];
 6         x[0] = ~0;
 7         for (int i = 0; i < A.length; i++) {
 8             t = x[k-1];
 9             for (int j = k-1; j > 0; j--) {
10                 x[j] = (x[j-1] & A[i]) | (x[j] & ~A[i]);
11             }
12             x[0] = (t & A[i]) | (x[0] & ~A[i]);
13         }
14         return x[l];
15     }
16 }

 Another solution, easier to figure out:

https://discuss.leetcode.com/topic/43166/java-o-n-easy-to-understand-solution-easily-extended-to-any-times-of-occurance

The usual bit manipulation code is bit hard to get and replicate. I like to think about the number in 32 bits and just count how many 1s are there in each bit, and sum %= 3 will clear it once it reaches 3. After running for all the numbers for each bit, if we have a 1, then that 1 belongs to the single number, we can simply move it back to its spot by doing ans |= sum << i;

This has complexity of O(32n), which is essentially O(n) and very easy to think and implement. Plus, you get a general solution for any times of occurrence. Say all the numbers have 5 times, just do sum %= 5.

public int singleNumber(int[] nums) {
    int ans = 0;
    for(int i = 0; i < 32; i++) {
        int sum = 0;
        for(int j = 0; j < nums.length; j++) {
            if(((nums[j] >> i) & 1) == 1) {
                sum++;
                sum %= 3;
            }
        }
        if(sum != 0) {
            ans |= sum << i;
        }
    }
    return ans;
}

 

转载于:https://www.cnblogs.com/lishiblog/p/4179356.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值