【刷题-LeeetCode】260. 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.
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?
解 在只有一个出现一次,其余数字出现两次的数组里面找出现一次的数字通过位异或即可实现。如果能将本题中的两个数字区分开来,问题也就可以解决了。思路同样是异或
- step1:生成mask,将所有的数字都进行异或,最后的结果就是两个出现了一次的数字(记为a, b)的异或结果,这两个数字至少有1位不相等,将异或结果的出现为1的位置作为mask,mask的二进制只有1位是1
- step2:寻找a和b,对于数组中的数字x,如果x&mask是1,说明x可能是a(或b);如果x&mask是0,说明x可能是b(或a)
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int x_or = 0;
for(auto x: nums)x_or ^= x;
int mask = 1;
while((mask & x_or) == 0)mask <<= 1;
int ans1 = 0, ans2 = 0;
for(auto x : nums){
if((mask & x) != 0){
ans1 ^= x;
}else{
ans2 ^= x;
}
}
return {ans1, ans2};
}
};
【刷题-LeeetCode】260. 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)
- [LeetCode] 260. Single Number III 单独数 III
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 ...
- [LeetCode#260]Single Number III
Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...
- 【leetcode】260. 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 II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
随机推荐
- CF1490D Permutation Transformation 题解
Content 给定一个排列 \(a\),按照以下方法构造一棵树: 选择当前排列中的最大数作为根的编号. 最大数左边的所有数按照上述方法建左子树,若没有数则该节点没有左儿子. 最大数右边的所有数按照上 ...
- Vue3.0是如何变快的
1.diff算法优化 + Vue2中的虚拟dom是进行全量的对比 https://vue-next-template-explorer.netlify.app/ + Vue3新增了静态标记(Patch ...
- SpringBoot简单整合Actuator监控
pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...
- JAVA使用经纬度通过腾讯地图API获取详细地址
官方文档:https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder package com.weixin.map; ...
- 【LeetCode】1579. 保证图可完全遍历 Remove Max Number of Edges to Keep Graph Fully Traversable
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 并查集 代码 欢迎加入组织 日期 题目地址:https ...
- 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...
- NetCore实现全局模型绑定异常信息统一处理
本文主要讲解NetCore如何使用中间件捕获模型绑定的异常信息 场景 在.NET Core 中请求中,如果参数的类型错误,我们在控制器的定义的方法是不会执行的,当我们需要捕获模型绑定的异常信息时,可以 ...
- 网站迁移纪实:从Web Form 到 Asp.Net Core (Abp vNext 自定义开发)
问题和需求 从2004年上线,ZLDNN.COM运行已经超过16年了,一直使用DotNetNuke平台(现在叫DNN Platform),从最初的DotNetNuke 2.1到现在使用的7.4.先是在 ...
- [android]打印C++的输出信息在安卓logcat上调试
#include <android/log.h> //宏定义全局函数:C++打印log到android-debug模式下帮助调试(勿删) //调用方式:slogd("test n ...
- Java基础周测一、二(50题)
一.单选题 (共50题,250分) 1.下列选项不可作为Java语言变量名的是( ). A. a1 B. $1 C. _1 D. 21 正确答案: D 2.有一段Java应用程序,它的类名是a1 ...