码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Single Number II

时间:2015-08-19 00:10:08      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

 

[LeetCode][JavaScript]Single Number II

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4740899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!