LeetCode Single Number III
原题链接在这里: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.
Example:
Input:[1,2,1,3,2,5]
Output:[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?
题解:
思路是逐个xor, 得到的结果是两个出线单次的数字a 和 b 的异或,axorb. a 与 b 不相同,所以a, b 取 xor肯定不为0,通过axorb $ (-axorb)找到 axorb中从右往左第一个不为0的digit.
通过这个digit把原来的nums 数组分成两类,一类这个digit上是0一类这个digit上是1. res[0] 来 xor 第一类, res[1] 来 xor 第二类.
Note: 位运算级别很低,需要用() 保护。
Time Complexity: O(nums.length).
Space: O(1).
AC Java:
public class Solution {
public int[] singleNumber(int[] nums) {
int [] res = {0,0};
if(nums == null || nums.length == 0){
return res;
}
//internal result a XOR b
int axorb = 0;
for(int i = 0; i<nums.length; i++){
axorb ^= nums[i];
} //from right to left, mark first digit that a and b are different
//通过这个mark把 原nums 数组分成两类,一类这个digit上是1,一类这个digit上是0.
int mark = axorb & (-axorb);
for(int i = 0; i<nums.length; i++){
if((nums[i] & mark) != 0){
res[0] ^= nums[i];
}else{
res[1] ^= nums[i];
}
}
return res;
}
}
类似Single Number, Single Number II.
LeetCode Single Number III的更多相关文章
- [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 ( 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——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- LeetCode Single Number III (xor)
题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- 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)
- 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] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- jQuery 写的插件图片上下切换幻灯效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- android之listview
首先建立res/layout/data_list.xml: 代码如下: <?xml version="1.0" encoding="utf-8"?> ...
- POJ 3628 Bookshelf 2(01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9488 Accepted: 4311 Descr ...
- nmap 高级扫描用法
nmap提供了四项基本功能(主机发现.端口扫描.服务与版本侦测.OS侦测)及丰富的脚本库.Nmap既能应用于简单的网络信息扫描,也能用在高级.复杂.特定的环境中:例如扫描互联网上大量的主机:绕开防火墙 ...
- MyBatis传入参数为集合 list 数组 map写法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- CentOS6.4下安装TeamViewer8
今天测试selenium调用firefoxdriver,该驱动无法在无界面环境中运行,需要远程连接到服务器进行操作,于是有了下面安装TeamViewer的过程. 先前尝试很多次也没有运行起来TeamV ...
- MySQL DATE_FORMAT() 函数
定义和用法 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的输出 ...
- Windows下查看机器监听端口
1.查看所有端口占用情况 在开始-运行-cmd,输入:netstat –ano可以查看所有进程 2.查看指定端口的占用情况 netstat -an |findstr :21
- c语言学习上的思考与心得
由于这段时间在c语言的学习中,表现的很努力并且完成作业态度认真,所以得到了老师奖励的小黄衫. 以下是我对于c语言的学习感受与心得. 学习感受与心得 我选择计算机的这个专业,是因为我对计算机的学习很有兴 ...
- CC254x(cc2540/cc2541)的微信AirSync调试笔记
一.前言 本尊自诩为IOT小能手,一直没涉足蓝牙实在说不过去.刚好上个月底的时候计划做个BLE设备,这阵子利用业余时间自学了BLE协议栈,了解了GATT,磕磕绊绊完成CC254x(cc2540/cc2 ...