LeetCode 【Single Number I II III】

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

思路:

最经典的方法,利用两个相同的数异或结果为0的性质,则将整个数组进行异或,相同的数俩俩异或,最后得到的就是那个single number,复杂度是O(n)

代码:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for( int i = 0; i < nums.size(); i++ ){
            res ^= nums[i];
        }
        return res;
    }
};

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

思路:


这题与上一题不同的地方在于,除了single number,其他数字都出现了3次,这样的话,整个异或就无法消去出现3次的数字,那我们有什么其他的办法呢?如果继续按照位来考虑,每一位可能出现的数字只有0或者1,而且如果某一位出现1,那么在这一位,所有数字(除了single num)出现1的次数总数一定是3的倍数,(比如某一位有5个不同数字是1,那么一共1在这一位出现了5*3 = 15次)那我们可以利用这个性质,统计每一位上1出现的总个数,然后总数除以3取它的余数(如果single number在这1位也是1,那么1一共出现16次,16%3 = 1),余数就是single num在这一位的数字。时间复杂度O(32N)

代码:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
    int count[32] = {0};
    int result = 0;
    for (int i = 0; i < 32; i++) {
        for (int j = 0; j < nums.size(); j++) {
            if ((nums[j] >> i) & 1) {
                count[i]++;
            }
        }
        result |= ((count[i] % 3) << i);
    }
    return result;
    }
};

思路2:  

那么有没有直接是O(N)的算法呢?答案是肯定的。这边使用了bitmask的概念,开始设3个flag,代表数字第一次出现,第二次出现,第三次出现如果出现1次,则one赋值,出现2次时候,one置0,twos赋值,出现3次时候,one,twos,都置0,threes赋值。具体可以参考https://discuss.leetcode.com/topic/2926/accepted-code-with-proper-explaination-does-anyone-have-a-better-idea/2

代码:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ones = 0, twos = 0, threes = 0;
        for (int i = 0; i < nums.size(); i++) {
             twos |= ones & nums[i];
             ones ^= nums[i];
             threes = ones & twos;
             ones &= ~threes;
             twos &= ~threes;
    }
    return ones;
    }
};

 

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

思路:

如果对所有元素进行异或,那么最后剩下的是两个single number的异或值,那么如何区分这两个single num呢?我们对最后的异或值进行分析,因为两个数字不同,那最后异或值一定不为0,如果某一位出现1表示这一位上,这两个数字是不同的,那我们就可以利用这个性质,通这一位,对整个数组进行分组,分为这一位是0的和这以为是1的,这样,我们就把这两个single number分到了两个数组中,问题就回到了single number1当中了。(via 剑指offer)

代码:

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int res = 0;
        for( int i = 0; i < nums.size(); i++ ){
            res ^= nums[i];
        }
        res &= -res;
    vector<int> resoult {0,0};
    for( int i = 0; i < nums.size(); i++ ){
        if( (nums[i] & res) == 0 ){
            resoult[0] ^= nums[i];
        }
        else{
            resoult[1] ^= nums[i];
        }
    }
        return resoult;
    }
};

  这边比较难理解的是res &= -res;加入res = 2则二进制表示为10; -2的二进制表示为1111111111111111111111111111111111111111111111111111111111111110,则最后位与值为2,其他的数同理,最后得到的是从低位向高位的第一位不同的值。

 

时间: 2024-08-03 17:52:25

LeetCode 【Single Number I II III】的相关文章

Leetcode 137. Single Number I/II/III

Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律. 1 class Solution(object): 2 def singleNumber(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz

Single Number i,ii,iii

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Bit: a ^ a = 0; public class Solution { public int singleNumber(int[] nums) { int res = 0; for(int n : nums){ res ^= n; } return res; } }

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you

[LeetCode] 260. Single Number III 单独数 III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is