[LeetCode][JavaScript]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.

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?

https://leetcode.com/problems/single-number-iii/



又是奇妙的位运算。

https://leetcode.com/discuss/52351/accepted-java-space-easy-solution-with-detail-explanations

第一轮遍历利用异或去掉出现两次的数,留下两个出现一次数的异或。

由于两个数不同,异或的结果中肯定有一位是1。

再遍历输入数组的时候,把输入的数与这一位做与运算,根据结果是否为零,可以把数组分为两份。

结果的两个数会被分开,异或去掉出现两次的数,最后得出答案。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[]}
 4  */
 5 var singleNumber = function(nums) {
 6     var two = 0, i;
 7     for(i = 0; i < nums.length; i++){
 8         two ^= nums[i];
 9     }
10     var pos = 1;
11     two = two.toString(2);
12     for(i = two.length - 1; i >= 0; i--){
13         if (two[i] === ‘1‘){
14             break;
15         }
16         pos *= 2;
17     }
18     var half1 = 0, half2 = 0;
19     for(i = 0; i < nums.length; i++){
20         if(nums[i] & pos){
21             half1 ^= nums[i];
22         }else{
23             half2 ^= nums[i];
24         }
25     }
26     return [half1, half2];
27 };
时间: 2024-12-11 13:56:20

[LeetCode][JavaScript]Single Number 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 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

[LeetCode][JavaScript]Single Number II

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 without using extra memory? https://leetcode.com/

[LeetCode][JavaScript]Single Number

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/problems/

Leetcode 260. Single Number III JAVA语言

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 

Leetcode 260 Single Number III 亦或

在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&(-a)就是计算出这个数,可以参考树状数组. 最后就是注意(nums[i] & a) == 0要加().注意符号运算优先级 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nu

[LeetCode] 260. Single Number III(位运算)

传送门 Description 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].

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you i

LeetCode 137 Single Number II(只出现一次的数字 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 im

LeetCode 136 Single Number(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo