Single Number III leetcode 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 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?
提示:bit manipulation
一、位运算
//参照single number 的方法,将所有数异或运算,得到result为两个single number的异或;
//找到result中最低位为1的那一位,是两个single number不同的地方,根据这个bit,可以将nums数组中的数分为A、B两组;
//然后分别在A和B中寻找single number即可
public static int[] singleNumber_Bit(int[] nums){ int result = 0;
for(int i = 0; i < nums.length; i++){
result = result ^ nums[i];
}
int[] res = new int[2];
//找到result二进制表示中最右侧的1
int pos = result & ( ~ (result - 1 )); //统计一个int型整数的二进制表示中有多少个1,可以采用 n = n & (n - 1);来从右到左逐个统计1的个数
for(int i = 0 ; i < nums.length; i++){ //将nums分为A B两组来分别求single number
if((pos & nums[i]) != 0){
res[0] = res[0] ^ nums[i];
} else {
res[1] = res[1] ^ nums[i];
}
}
return res;
}
Single Number III leetcode java的更多相关文章
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- Single Number III - LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 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 ...
- 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 ...
随机推荐
- Docker Tomcat远程部署到容器
一:创建一个开启远程部署的tomcat容器 tomcat角色配置 1.tomcat开启远程部署,修改conf/tomcat-users.xml <?xml version="1.0&q ...
- 18 Issues in Current Deep Reinforcement Learning from ZhiHu
深度强化学习的18个关键问题 from: https://zhuanlan.zhihu.com/p/32153603 85 人赞了该文章 深度强化学习的问题在哪里?未来怎么走?哪些方面可以突破? 这两 ...
- [jsp & thymeleaf] - jsp和thymeleaf的共存解析
做项目时因为有些老jsp还需要测试用到,所以之前的thymeleaf也需要保持,配置如下: https://github.com/deadzq/jsp-thymeleaf 等空余时间在做详解吧!
- MPU6050可以读取ID值,温度值和原始数据值为零问题解决
MPU6050可以读取ID值是0x68,但是读取到的原始数据为零(下面虚拟示波器图中温度值是36.529是单位转换公式中的值被打印出来了,实际值也是零).经论坛搜寻,发现MPU6050出现问题的原因有 ...
- 防止网站检测出Selenium的window.navigator.webdriver属性
只需在Chromeoptions对象中添加一个属性即可解决 import time from selenium.webdriver import Chrome, ChromeOptions optio ...
- Tomcat下webapps夹中root文件夹作用及如何发布项目至root文件夹中
转载请注明出处: tomcat的root文件夹下面默认是tomcat的管理程序,但是如果你把自己的web项目发布到root下面的话,你可以不通过项目名直接访问你的项目,比如,你见了一个名为Test的项 ...
- 51nod 1672 区间交(贪心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 题意: 思路:其实这就是一个经典的区间贪心问题,只需要按照左端点排 ...
- 2. mysql 语句
基础语句 创建表 DROP TABLE IF EXISTS student;CREATE TABLE student ( id ) NOT NULL AUTO_INCREMENT, sname ) N ...
- 如何终止线程的运行(C/C++)
想要终止线程的运行,可以使用以下方法: 1.线程函数返回(最好使用该方法). 2.通过调用ExitThread函数,线程将自行撤消(最好不使用该方法). 3.同一个进程或另一个进程中的线程调用Term ...
- 小程序之取标签中内容 例如view,text
// index.wxml页面 data-url为自定义 {{}}中内容可为后台请求到的数据 也可为固定内容例如:data-text="哈哈哈" data-url="ht ...