Single Number III

    • 题目描述
    • 链接地址
    • 解法
    • 算法解释

题目描述

Given 2*n + 2 numbers, every numbers occurs twice except two, find them.

Example

Given [1,2,2,3,4,4,5,3] return 1 and 5

Challenge

O(n) time, O(1) extra space.

.

链接地址

http://www.lintcode.com/en/problem/single-number-iii/

解法

   vector<int> singleNumberIII(vector<int> &A) {
        // write your code here
    int ret = 0;
    for (int i = 0; i < A.size(); i++) {
       ret = ret ^ A[i];
    }
    int lastOneVal = ret - (ret & (ret - 1));
    vector<int> vec1;
    vector<int> vec2;
    for (int i = 0; i < A.size(); i++) {
        if (A[i] & lastOneVal) {
            vec1.push_back(A[i]);
        }else {
            vec2.push_back(A[i]);
        }
    }
    int ret1 = 0;
    int ret2 = 0;
    for(int i = 0; i < vec1.size(); i++) {
       ret1 = ret1 ^ vec1[i];
    }
    for(int j = 0; j < vec2.size(); j++) {
      ret2 = ret2 ^ vec2[j];
    }
     vector<int> retVec;
     retVec.push_back(ret1);
     retVec.push_back(ret2);
     return retVec;
  }

算法解释

在前面解决过2*n+1的问题,现在面临的是2*n+2 的问题,我们想用什么方法可以将2*n+2的问题划分为两个集合转化为2*n+1,然后在各个集合采用异或操作,最后可得到两个值。

如何才能转化为2*n+1,只要解决这个,那么所有问题迎刃而解。

解决关键:所有数异或,得到最后结果,然后找最后一个非零位置。根据这个位置做分类,所有这个位置为零的为一组,所有这个位置为一的为一组。

找最后一个是1的位置,用算法:

C-(C-1)&C 得到最后一个非零位置组成的数

如C = 1100, 算出来的结果是0100.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 00:47:10

Single Number III的相关文章

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

[LeetCode][JavaScript]Single Number III

Single Number 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. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

260. Single Number III [medium] (Python)

题目链接 https://leetcode.com/problems/single-number-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. For example: Given

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

LeetCode OJ 之 Single Number 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. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

[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

Leetcode 260. Single Number III JAVA语言

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]. Note: The order