O(N), a few lines of codes


  • 0
    F

    This solution is based on some knowledge of Digital Logic Design.

    class Solution {
    public:
        int singleNumber(int A[], int n) {
            int curVal = 0;
            int curState = 0;
            for(int i=0; i<n; i++)
            {
                int temp1 = curVal & A[i] & (~curState);
                int temp2 = (~A[i]) & curState; 
                curVal = (curVal^A[i])&(~curState);
                curState = temp1|temp2;
            }
            return curVal;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.