lintcode 中等题:Single number III 落单的数III

题目

落单的数 III

给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字。

样例

给出 [1,2,2,3,4,4,5,3],返回 1和5

挑战

O(n)时间复杂度,O(1)的额外空间复杂度

解题

根据落单的数I,可以想到,所有的数进行异或运行的结果就是所求两个数的异或结果。

这个异或的结果,二进制数是1的位置说明这两个数对应的二进制位不相同。然后再怎么还原???
参考,理解的不是很透,找到第k位后,再判断数组中所以数的第k位是0 还是1,,出现两次的数对求解无影响,通过这个第k为把数组分成两类,也就把两个数分开了,这里的第k位在a、b中一定不相同的,一定是一个0一个1。

public class Solution {
    /**
     * @param A : An integer array
     * @return : Two integers
     */
    public List<Integer> singleNumberIII(int[] A) {
        // write your code here
        int axorb = 0;
        LinkedList<Integer> res = new LinkedList<Integer>();
        for( int i = 0; i <A.length;i++){
            axorb ^= A[i];
        }
        int a = 0;
        int b = 0;
        int k = 0;
        while( axorb % 2==0){
            axorb >>= 1;
            k++;
        }
        for(int i=0;i< A.length;i++){
            int tmp =( A[i]>>k)%2;
            if(tmp==0)
                a ^= A[i];
            else
                b ^= A[i];
        }
        res.add(a);
        res.add(b);
        return res;
    }
}

Java Code

总耗时: 3520 ms

class Solution:
    """
    @param A : An integer array
    @return : Two integer
    """
    def singleNumberIII(self, A):
        # write your code here
        x = 0
        for num in A:
            x ^= num
        a = 0
        b = 0
        k = 0
        while x%2==0:
            x = x>>1
            k +=1
        for num in A:
            tmp = (num>>k)%2
            if tmp==0:
                a ^=num
            else:
                b ^=num
        return [a,b]

Python Code

总耗时: 514 ms

当然对于这样的题目,利用HashMap是最简单不过的了。

public class Solution {
    /**
     * @param A : An integer array
     * @return : Two integers
     */
    public List<Integer> singleNumberIII(int[] A) {
        // write your code here
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        LinkedList<Integer> res = new LinkedList<Integer>();
        for(int i=0;i<A.length;i++){
            if(map.containsKey(A[i])){
                map.put(A[i],map.get(A[i]) + 1);
            }else{
                map.put(A[i],1);
            }
            if(map.get(A[i]) ==2)
                map.remove(A[i]);
        }
        for(Integer k:map.keySet()){
            res.add(k);
        }
        return res;
    }
}

Java Code

总耗时: 4318 ms

优化一下

public class Solution {
    /**
     * @param A : An integer array
     * @return : Two integers
     */
    public List<Integer> singleNumberIII(int[] A) {
        // write your code here
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        LinkedList<Integer> res = new LinkedList<Integer>();
        for(int i=0;i<A.length;i++){
            if(map.containsKey(A[i])){
                 map.remove(A[i]);
            }else{
                map.put(A[i],1);
            }
        }
        for(Integer k:map.keySet()){
            res.add(k);
        }
        return res;
    }
}

Java Code

总耗时: 3995 ms

class Solution:
    """
    @param A : An integer array
    @return : Two integer
    """
    def singleNumberIII(self, A):
        # write your code here
        d = {}
        for num in A:
            if num in d:
                del d[num]
            else:
                d[num] = 1
        return d.keys()

Python Code

总耗时: 586 ms

时间: 2024-08-05 20:09:44

lintcode 中等题:Single number III 落单的数III的相关文章

LintCode Python 简单级题目 82.落单的数

题目描述: 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 您在真实的面试中是否遇到过这个题? Yes 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 标签 贪心 题目分析: 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 利用  n^n = 0的特性,一个数异或本身结果为0 而  n^0 = n,所以 n^n-1^n = n-1, 所以保存异或结果,循环异或列表元素即可

lintcode 落单的数(位操作)

题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single-number/ 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 解决方案 方法1思路:将所有的数转换成二进制,因为是int类型,共32位.申请常数级(32位)的额外空间,然后每个数对应的位相加,最后对应位上的和模2.最后的结果就是单个数对应的二进制数.

落单的数

.title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 } .todo { font-family: monospace; color: red } .done { font-family: monospace; color: green } .priority { font-fami

codevs 3295 落单的数

题目描述 Description 有n个数(n是奇数),其中n-1个数两两成对,有1个数落单,找出这个数.要求O(n)的时间复杂度,O(1)的空间复杂度 输入描述 Input Description 第一行输入一个n, n是大于等于1的奇数 第二行包含n个整数 输出描述 Output Description 输出那个落单的数 样例输入 Sample Input 3 1 7 1 样例输出 Sample Output 7 数据范围及提示 Data Size & Hint 1<=n<=400

leetcode第136题-Single Number

题目要求:给出一个数组,只有一个数字出现一次,其他的都出现两次,找出那出现一次的数字,要求用线性的时间解出题目! 分析:因为题目要求的是用线性时间,所以类似于那种暴力解决的方法会超时,如下面这种: int singleNumber2(int *nums,int n) { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(nums[i]==nums[j]) break; else continue; } if(j==n) return num

[容易]落单的数

题目来源:http://www.lintcode.com/zh-cn/problem/single-number/ 方法: 异或满足交换律,任意两个相同的数可以异或是0.0和任何数异或的结果是该数,那么最后的结果一定是落单的数字. C++版 VS2012测试通过: 1 //#include <iostream> 2 //#include <vector> 3 //#include <queue> 4 //#include <algorithm> 5 //us

lintcode 中等题:find the missing number 寻找缺失的数

题目 寻找缺失的数 给出一个包含 0 .. N 中 N 个数的序列,找出0 .. N 中没有出现在序列中的那个数. 样例 N = 4 且序列为 [0, 1, 3] 时,缺失的数为2. 注意 可以改变序列中数的位置. 挑战 在数组上原地完成,使用O(1)的额外空间和O(N)的时间. 解题 重新定义一个数组存放排序后的数,空间复杂度和时间复杂度都是O(N) public class Solution { /** * @param nums: an array of integers * @retur

lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 注意 以上的答案是按照词

lintcode 中等题:continuous subarray sum 联系子数组之和

题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i