博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Single Number III
阅读量:4662 次
发布时间:2019-06-09

本文共 1008 字,大约阅读时间需要 3 分钟。

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?

 

Credits:

Special thanks to  for adding this problem and creating all test cases.

x &= -x 是用了树状数组中的lowbit。

1 class Solution { 2 public: 3     vector
singleNumber(vector
& nums) { 4 int x = 0; 5 for (auto n : nums) x ^= n; 6 int tmp = x, idx = 0; 7 x &= -x; 8 int a = 0, b = 0; 9 for (auto n : nums) {10 if (n & x) a ^= n;11 else b ^= n;12 }13 return {min(a, b), max(a, b)};14 }15 };

 

转载于:https://www.cnblogs.com/easonliu/p/4811935.html

你可能感兴趣的文章
计算一个算数表达式的值
查看>>
hdu squarefree number
查看>>
atc-前端模板预编译器
查看>>
SDF(Signed-distance-field: 有向距离场)(7): 距离场函数-基于CUBE计算方式产生的若干变体A...
查看>>
poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
查看>>
Codeforces Round #194 (Div. 1) A. Secrets 数学
查看>>
看不懂 ASP.NET 相册上传代码
查看>>
redis+mysql
查看>>
IIS中找不到dll文件的依赖项问题
查看>>
Loadrunner的stock和web协议对应的事务检查点
查看>>
mvc扩展HtmlHelper功能
查看>>
codeforces #541 D. Gourmet choice(拓扑+并查集)
查看>>
ocilib linux编译安装
查看>>
linux 链接库说明
查看>>
基于本地文件系统的LocalDB
查看>>
黑马程序员 java基础加强--类加载器
查看>>
Win10环境下安装Django
查看>>
[Leetcode] Permutations
查看>>
mysqlbinlog flashback 5.6完全使用手册与原理
查看>>
1-1 07:输出浮点数
查看>>