Leetcode260 Single Number III

告别抓瞎!手把手教你用Keysight Infinium和M8040完成PCIe 5.0 TX/RX信号完整性测试 本文详细介绍了如何使用Keysight Infinium示波器和M8040误码仪完成PCIe 5.0 TX/RX信号完整性测试。从测试前的设备准备、软件配置到具体的TX信号测试步骤和RX容限测试实战,提供了全面的操作指南和常见问题解决方案,帮助工程师高效完成PCIe 5.0合规性测试。 阅读详情

leetcode代码已经放入github:[https://github.com/gaohongbin/leetcode](https://github.com/gaohongbin/leetcode)
题目:
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?

翻译:
给一个数组,在这个数组中,只有两个数只出现了一次,其他所有的数都在数组中出现了两次。找出这两个只出现了一次的数。

小提示:
1.结果中两个数的顺序没有要求。
2.你的算法时间复杂度可以为O(n)吗?你的空间复杂度可以为O(1)吗?

思路:
假设数组中只出现一次的数分别为A,B;
我们都知道 X^X=0; 所以如果将数组中的数字全部进行异或处理,则结果与 A^B相等. 而且因为A!=B,则A^B!=0.
则A,B用二进制表示时1的位置也不尽相同。
N&(-N)的结果为N的最右边的1和后面所有0所组成的整数。
例: N=6 即(0110)
则 6 &(-6) =2;

因为这个1是从A^B中找出来的,则A和B的二进制在该位置上肯定一个为0,一个为1.

依据这样就可以把整个数组分为两大类,(1)该二进制位为1的数
(2)该二进制位为0的数

现在我们只需要在这被划分的两个数组中,分别找出只出现一次的那个数即可。

代码:

public int[] singleNumber(int[] nums) {
            int length=nums.length;
            int AxorB=0;

            for(int i=0;i<length;i++){
                AxorB=AxorB^nums[i];
            }

            int mask=AxorB & (-AxorB);
            int A=0,B=0;
            for(int i=0;i<length;i++){
                if((mask & nums[i])==0)
                    A^=nums[i];
                else
                    B^=nums[i];
            }

            int[] singleNumber=new int[]{A,B};
            return singleNumber;
        }
解放双手:用KUKA示教器白键一键触发复杂工艺,自定义你的快捷指令 本文详细介绍了如何利用KUKA示教器的工艺白键功能,通过自定义快捷指令一键触发复杂工艺,显著提升自动化产线效率。文章涵盖UserTech技术栈解析、KRL脚本封装、人机交互视觉工程及工业实战策略,帮助工程师实现机械臂操作的高阶优化。 阅读详情

相关推荐

手把手教你用smarteye搭建免费GB28181监控平台(含海康设备接入避坑指南)

本文提供了一份基于SmartEye搭建免费GB28181视频监控平台的实战指南。详细介绍了从服务器部署、GB28181协议核心配置,到海康威视等主流设备接入的完整流程,并重点分享了设备注册失败、无法预览等常见问题的深度排错方法,旨在帮助中小企业及开发者低成本实现多品牌监控设备的统一管理。

pz89012345的博客 294

leetcode 260.Single Number III

leetcode的第260Single Number III 中有位大神的解题方法。

程序猿成长日志 406

关于 SAP UI5 应用 url 中的特殊符号 # 的作用介绍

符号在 SAP UI5 的 URL 中不仅仅是一个分隔符,更是动态路由、状态管理和深链接的关键所在。通过合理使用,开发者可以实现高效、流畅的用户体验,同时保持系统的可维护性和扩展性。通过 SAP UI5 提供的灵活路由机制,企业应用可以轻松实现复杂导航需求,同时为用户提供直观的交互体验。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 371

LeetCode: 260. Single Number III

LeetCode: 260. 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 ...

杨领well的专栏 196

[leetcode]260. Single Number III

[leetcode]260. Single Number III Analysis 心态奶茶妹妹~—— [马云爸爸恐成最大赢家 哈哈哈] Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly ...

weixin_32135877的博客 191

leetcode260Single Number III

写在最前面: 当然可以通过遍历查找,但是时间复杂度太高,leetcode肯定通不过。 然后我们要研究异或。   leetcode260Single Number III Given an array of numbers nums, in which exactly two elements appear only once and all the other elements a...

ssjdoudou的博客 2万+

LeetCode 260. Single Number III 题解(C++)

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.

prayyojay的博客 492

LeetCode 260. Single Number III解题报告(python)

260. Single Number III Lowest Common Ancestor of Deepest Leaves python solution 题目描述 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear ...

Orientliu96的博客 361

LeetCode 260.Single Number III(只出现一次的数 III)

题目 LeetCode: 260. Single Number III 力扣: 260. 只出现一次的数字 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. 给定一个整数数组 nums,其中恰好有两个元素只出

TomAndersen的博客 322

LeetCode 260. Single Number III 解题报告

LeetCode 260. Single Number III 解题报告

骆小坑的填坑之旅 1080

[LeetCode 260] 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. For example: Given nums =

coder 进阶的专栏 1万+

[leetcode] 260.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.For example:Given nums =

u014673347的专栏 2624

leetcode123 Best Time to Buy and Sell Stock III

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note:

每天进步一点点 2037

Leetcode75 Sort Colors

leetcode代码已经放入github:[https://github.com/gaohongbin/leetcode](https://github.com/gaohongbin/leetcode) 题目: Given an array with n objects colored red, white or blue, sort them so that objects of the sa

每天进步一点点 900

Leetcode39:Combination Sum

题目:给定一个整型数组candidates,和一个目标值target,使用数组中的数组成target,找出所有的组合。例如: candidates = {2,3,6,7}; target =7 ; 则返回的结果为:[[7],[2,2,3]] 。 题目说明,candidates中的数字全为正数,target也全为正数。 并且返回的结果中candidates中的数可以是重复的,而且不限次数的重复。但每

每天进步一点点 791

leetcode122 Best Time to Buy and Sell Stock II

题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i

每天进步一点点 663

leetcode229. Majority Element II

/**  * 题目:如果数组数组中有一个数超过三分之一,则称为主数,现在寻找数组中的所有主数。  * @author hongbin.gao  * 思路:原来有一道题目,主数的定义为超过二分之一的数,我们的思路是,将数组中的数两两一组(两个数不等),则最后余下的那个数可能是主数,如果题目说一定存在主数,那就不用检查了,如果不一定存在,则还需要检查一下。  * 现在我们采用相同的思路,将三个

每天进步一点点 633

leetcode137 Single Number II

题目: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wi

每天进步一点点 605

leetcode155 Min Stack

题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top()

每天进步一点点 566

Leetcode231 Power of Two

leetcode231

每天进步一点点 560

leetcode55 Jump Game

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. De

每天进步一点点 555

leetcode232 Implement Queue using Stacks

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme

每天进步一点点 531

IB Specification 2.1

IB Specification

LabVIEW通过1200以太网.rar_Labview与PLC通讯_PLC 以太网_labview TCP_labview p

西门子S7-1200 PLC支持多种协议通过以太网通讯方式,如OPC、TCP、S7、MODBUS-TCP等。TCP方式相比其它协议,具有更多的灵活性,更快的数据响应等优点,本人即介绍LabVIEW通过TCP方式与S7-1200 PLC通信。 S7-1200 PLC的TCP通信方式,称之为“开放式用户通讯”,通过以太网发送或读取数据。

上一篇: Leetcode50 Pow(x, n)
下一篇: Leetcode75 Sort Colors
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值