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 once.

For example:

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

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

Code:

  1. vector<int> singleNumber(vector<int>& nums) {
  2. vector<int>result;
  3. if (nums.size() < )
  4. return result;
  5. int xorAll = ;
  6. for (int i = ; i < nums.size(); ++i)
  7. xorAll^=nums[i];
  8. unsigned int x = ;
  9. while ((xorAll & x) == )
  10. {
  11. x=x<<;
  12. }
  13. int result1=,result2=;
  14. for (int j = ; j < nums.size(); ++j)
  15. {
  16. if ((nums[j]&x) == )
  17. result1^=nums[j];
  18. else
  19. result2^=nums[j];
  20. }
  21. result.push_back(result1);
  22. result.push_back(result2);
  23. return result;
  24. }

PS:

思路很清晰,但是写代码的是后老在细节出错,主要是两个判断条件要注意

Single Number III的更多相关文章

  1. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  2. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  3. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  4. Single Number III(LintCode)

    Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...

  5. 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 ...

  6. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

  7. [LeetCode] Single Number III 单独的数字之三

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

  8. LeetCode Single Number III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

  9. [LeetCode] Single Number III ( a New Questions Added today)

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

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

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

随机推荐

  1. AndroidManifest.xml 详解

    第1部分 标签库+包路径+版本控制 <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...

  2. Resilio(BtSync)搭建

    Resilio(原名:BtSync)介绍 同步是使用PC和Mac,NAS,甚至服务器之间传输文件的最好方法.创建自己的私有云.连接设备和同步文件安全,不发送他们在第三方服务器.我们不限制你的速度和存储 ...

  3. 某些输入文件使用或覆盖了已过时的 API

    android出现注: 某些输入文件使用或覆盖了已过时的 API. 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译. 注: 某些输入文件使用了未经检查或不安全的操作. 注 ...

  4. C#和SQl 注入字符串的攻击 和 防止注入字符转的攻击

    --SQl中 --建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) primary key, ...

  5. Jetty和Tomcat的选择:按场景而定

    Jetty和Tomcat的选择:按场景而定 Jetty和Tomcat为目前全球范围内最著名的两款开源的webserver/servlet容器.由于它们的实现都遵循Java Servlet规范,一个Ja ...

  6. Duilib自定义控件响应指定命令(转载)

    转载:http://blog.csdn.net/panxianzhan/article/details/50772893 duilib在UIManager.h里的EVENTTYPE_UI枚举里定义了很 ...

  7. 高通平台 lcd driver 调试小结

    一.概述 1.1 简介 本文档主要包括LCD模块的驱动流程分析.Framebuffer相关知识.Gralloc等相关内容,以及LCD调试的一些经验和相关bug的分析和讲解. 1.2  开发环境 And ...

  8. 关于三星A7屏幕锁已由管理员、加密政策,或证书存储禁用

    解决办法:设定-安全-清除证书-再返回锁定屏幕-把密码锁定-改为滑动.....

  9. Android-activity-intent

    package com.hanqi.myapplication; import android.content.ComponentName; import android.content.Intent ...

  10. TreeList的使用

    添加列 TreeListColumn column = treeList1.Columns.Add(); column.Caption = @"建筑列表"; column.Visi ...