[LeetCode] 260. Single Number III 单独数 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:
[1,2,1,3,2,5]
[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?
这次的问题变成special的数又两个了,实际上题目并不是找single number了,而是找special two number。所以之前两个题目的解法是不适用的,起码不是直接适用的。如果可以把two number的问题变成两个single number的问题,就可以套用之前第一个问题的解法了,也就是说我们可以通过某种方式把数组分为两组,每组只包含那两个special number中的一个。
问题的关键就变成如何分组了。思路也是有点巧妙,考虑到两个special number是不一样的,而恰好其余的数都是出现两次,所以如果对每个数都做亦或操作,最后的结果就是那两个special number的亦或,而且至少有一个位是1,那么就可以根据其中一个为1的位将所有的数分为两组,再套用第一个题的方法即可。
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class Solution { public int [] singleNumber( int [] nums) { // Pass 1 : // Get the XOR of the two numbers we need to find int diff = 0 ; for ( int num : nums) { diff ^= num; } // Get its last set bit diff &= -diff; // Pass 2 : int [] rets = { 0 , 0 }; // this array stores the two numbers we will return for ( int num : nums) { if ((num & diff) == 0 ) // the bit is not set { rets[ 0 ] ^= num; } else // the bit is set { rets[ 1 ] ^= num; } } return rets; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Solution( object ): def singleNumber( self , nums): """ :type nums: List[int] :rtype: List[int] """ xor = 0 a = 0 b = 0 for num in nums: xor ^ = num mask = 1 while (xor&mask = = 0 ): mask = mask << 1 for num in nums: if num&mask: a ^ = num else : b ^ = num return [a, b] |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Solution { public : vector< int > singleNumber(vector< int >& nums) { // Pass 1 : // Get the XOR of the two numbers we need to find int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor< int >()); // Get its last set bit diff &= -diff; // Pass 2 : vector< int > rets = {0, 0}; // this vector stores the two numbers we will return for ( int num : nums) { if ((num & diff) == 0) // the bit is not set { rets[0] ^= num; } else // the bit is set { rets[1] ^= num; } } return rets; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : vector< int > singleNumber(vector< int >& nums) { int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor< int >()); diff &= -diff; vector< int > res(2, 0); for ( auto &a : nums) { if (a & diff) res[0] ^= a; else res[1] ^= a; } return res; } }; |
类似题目:
[LeetCode] 136. Single Number 单独数
[LeetCode] 137. Single Number II 单独数 II
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架