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 once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
Note:
- The order of the result is not important. So in the above example,
[5, 3]
is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Code:
- vector<int> singleNumber(vector<int>& nums) {
- vector<int>result;
- if (nums.size() < )
- return result;
- int xorAll = ;
- for (int i = ; i < nums.size(); ++i)
- xorAll^=nums[i];
- unsigned int x = ;
- while ((xorAll & x) == )
- {
- x=x<<;
- }
- int result1=,result2=;
- for (int j = ; j < nums.size(); ++j)
- {
- if ((nums[j]&x) == )
- result1^=nums[j];
- else
- result2^=nums[j];
- }
- result.push_back(result1);
- result.push_back(result2);
- return result;
- }
PS:
思路很清晰,但是写代码的是后老在细节出错,主要是两个判断条件要注意
Single Number III的更多相关文章
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- 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 ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [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 ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- AndroidManifest.xml 详解
第1部分 标签库+包路径+版本控制 <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...
- Resilio(BtSync)搭建
Resilio(原名:BtSync)介绍 同步是使用PC和Mac,NAS,甚至服务器之间传输文件的最好方法.创建自己的私有云.连接设备和同步文件安全,不发送他们在第三方服务器.我们不限制你的速度和存储 ...
- 某些输入文件使用或覆盖了已过时的 API
android出现注: 某些输入文件使用或覆盖了已过时的 API. 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译. 注: 某些输入文件使用了未经检查或不安全的操作. 注 ...
- C#和SQl 注入字符串的攻击 和 防止注入字符转的攻击
--SQl中 --建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) primary key, ...
- Jetty和Tomcat的选择:按场景而定
Jetty和Tomcat的选择:按场景而定 Jetty和Tomcat为目前全球范围内最著名的两款开源的webserver/servlet容器.由于它们的实现都遵循Java Servlet规范,一个Ja ...
- Duilib自定义控件响应指定命令(转载)
转载:http://blog.csdn.net/panxianzhan/article/details/50772893 duilib在UIManager.h里的EVENTTYPE_UI枚举里定义了很 ...
- 高通平台 lcd driver 调试小结
一.概述 1.1 简介 本文档主要包括LCD模块的驱动流程分析.Framebuffer相关知识.Gralloc等相关内容,以及LCD调试的一些经验和相关bug的分析和讲解. 1.2 开发环境 And ...
- 关于三星A7屏幕锁已由管理员、加密政策,或证书存储禁用
解决办法:设定-安全-清除证书-再返回锁定屏幕-把密码锁定-改为滑动.....
- Android-activity-intent
package com.hanqi.myapplication; import android.content.ComponentName; import android.content.Intent ...
- TreeList的使用
添加列 TreeListColumn column = treeList1.Columns.Add(); column.Caption = @"建筑列表"; column.Visi ...