当前位置:网站首页>260. Single Number III
260. Single Number III
2022-06-22 12:26:00 【Sterben_Da】
260. Single Number III
Medium
3945186Add to ListShare
Given an integer array 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. You can return the answer in any order.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Example 1:
Input: nums = [1,2,1,3,2,5] Output: [3,5] Explanation: [5, 3] is also a valid answer.
Example 2:
Input: nums = [-1,0] Output: [-1,0]
Example 3:
Input: nums = [0,1] Output: [1,0]
Constraints:
2 <= nums.length <= 3 * 104-231 <= nums[i] <= 231 - 1- Each integer in
numswill appear twice, only two integers will appear once.
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
"""
sorted(Solution().singleNumber([1, 2, 1, 3, 2, 5])) == [3, 5]
参考别人的解题思路:假设a,b为单独的2个数,对nums所有数进行异或,可以得到 d=a^b 的值
方法1:找到d中第一个二进制位为1的位数k,再次对nums数组的数在位数k为1或者为0进行分组,a,b肯定落在单独的一边
int k = 0;
while(!(sum >> k & 1)) k++;
if(x >> k & 1)
采用方法2:d &(-d), 可以找到最低位为1的数,接着同方法1分组
时间复杂度:O(n) 空间复杂度:O(1)
如 a=3,b=5 d=a^b=6 d &(-d)= 2 可以找到最低位为1的数
正数转负数 总结过程: 6 = 0 110 (原码)
a.最高位改成1 1 110 负数二进制表示
b.除了最高位,其他位取反 1 001
c.结果+1 1 010
d.得到的结果就是对应的负值 -6 = 1 010 负数在机器中以补码形式保存
负数转正数反之
"""
d = 0
for i in nums:
d ^= i
# 找到最低位为1的数
d &= -d
result = [0, 0]
for i in nums:
if i & d == 0:
result[0] ^= i
else:
result[1] ^= i
return result边栏推荐
- Think PHP environment construction notes
- Sequoiadb distributed database may 2022 issue
- Final of the 11th Blue Bridge Cup embedded design and development project
- Parallels Desktop 17.1.4pd虚拟机
- Arcpy 添加图层到地图文档
- [MySQL] the difference between where and having
- MySQL笔记
- 【Qt】Qt获取标准系统路径
- Recommend a virtual machine software for fast cluster building of M1 chip computers
- 天翼云探索云原生、边缘计算融合新思路
猜你喜欢

动作捕捉系统用于地下隧道移动机器人定位与建图

Flutter mixed development exercise - large picture of collaborative loading of ever & method channel

AcWing 241 楼兰图腾(树状数组详解)

universaldependencies依存关系标签解释

Final of the 11th Blue Bridge Cup embedded design and development project

docker安装postgresql

Tis tutorial 01- installation

2022-6-21OS复习成组链接法

天翼云数字政府智慧数据中台通过认证

Parallels Desktop 17.1.4pd virtual machine
随机推荐
0179 largest number
Wisdom age voice +php
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
Reconstruction practice of complex C-end project of acquisition technology
Recommend a virtual machine software for fast cluster building of M1 chip computers
信创之下:国产数据库群星闪耀时
Reddit product director: a practical guide for NFT members for Web3 creators
Sap-abap-se14 how to recover lost data
SAP 开发Keys 申请SSCR Keys申请
Tis tutorial 03 export
0007 reverse integer
SNC processing failed SAP router certificate regeneration
SAP development keys application SSCR keys application
PostGIS through St_ Dwithin retrieves elements within a certain distance
The solution of VPC network automatic configuration based on terraform
Pycharm shell script cannot be run
SICF批量激活服务节点
Windows system installs multiple MySQL versions (without uninstalling the original version), and the old and new versions are compatible.
Precautions for upgrading php8 of diyun CMS
Making rectangular border according to metric system through PostGIS