[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/problems/single-number-ii/



这种位运算的题超越了我的三观,根本不可能。

https://leetcode.com/discuss/857/constant-space-solution

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var singleNumber = function(nums) {
 6     var ones = 0, twos = 0, threes = 0;
 7     for(var i = 0; i < nums.length; i++){
 8         twos |= ones & nums[i];
 9         ones ^= nums[i];
10         threes = ones & twos;
11         ones &= ~threes;
12         twos &= ~threes;
13     }
14     return ones;
15 };
时间: 2024-10-26 20:19:34

[LeetCode][JavaScript]Single Number II的相关文章

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/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 co

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】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:开辟map记录次数 class So

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

[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][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]

Leetcode 137. Single Number II JAVA语言

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题意:一个数组中除

【LeetCode】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? 思路: 用 ones, twos, threes 分别记录数字在二

Java [Leetcode 137]Single Number II

题目描述: Given an array of integers, every element appears three times except for one. Find that single one. 解题思路: 具体参考Detailed explanation and generalization of the bitwise operation method for single numbers 讲的实在是很全面,而且拓展到了一般的情况,膜!! 代码如下: public class