Skip to content

Commit 63ae02f

Browse files
author
zongyanqi
committed
add Easy_136_Single_Number Easy_190_Reverse_Bits Easy_191_Number_of_1_Bits
1 parent f1cdbbc commit 63ae02f

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

Easy_136_Single_Number.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/single-number/#/description
4+
*
5+
* Given an array of integers, every element appears twice except for one. Find that single one.
6+
*
7+
* Note:
8+
* Your algorithm should have a linear runtime complexity.
9+
*
10+
* Could you implement it without using extra memory?
11+
*
12+
*
13+
* https://discuss.leetcode.com/topic/1916/my-o-n-solution-using-xor/1
14+
* Hint: known that A XOR A = 0 and the XOR operator is commutative
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
* @return {number}
20+
*/
21+
var singleNumber = function (nums) {
22+
23+
return nums.reduce((ret, n) => ret ^= n, 0);
24+
25+
};
26+
27+
console.log(singleNumber([1, 2, 2]) == 1);
28+
console.log(singleNumber([1, 2, 1]) == 2);

Easy_190_Reverse_Bits.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-bits/#/description
3+
*
4+
* Reverse bits of a given 32 bits unsigned integer.
5+
*
6+
* For example, given input 43261596 (represented in binary as 00000010100101000001111010011100),
7+
*
8+
* return 964176192 (represented in binary as 00111001011110000010100101000000).
9+
*
10+
* 00000010100101000001111010011100
11+
* 00111001011110000010100101000000
12+
*/
13+
14+
/**
15+
* @param {number} n - a positive integer
16+
* @return {number} - a positive integer
17+
*/
18+
var reverseBits = function (n) {
19+
// reverse binary str
20+
var str = '';
21+
var i = 32;
22+
while (i--) {
23+
str += n % 2;
24+
n = Math.floor(n / 2);
25+
}
26+
27+
return parseInt(str, 2);
28+
};
29+
30+
console.log(reverseBits(43261596) == 964176192);

Easy_191_Number_of_1_Bits.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*
3+
* https://leetcode.com/problems/number-of-1-bits/#/description
4+
*
5+
* Write a function that takes an unsigned integer and returns the number of ’1' bits it has
6+
* (also known as the Hamming weight).
7+
*
8+
* For example, the 32-bit integer ’11' has binary representation
9+
* 00000000000000000000000000001011, so the function should return 3.
10+
*/
11+
12+
/**
13+
* @param {number} n - a positive integer
14+
* @return {number}
15+
*/
16+
var hammingWeight = function (n) {
17+
var weight = 0;
18+
19+
while (n > 0) {
20+
weight += n % 2;
21+
n = Math.floor(n / 2);
22+
}
23+
return weight;
24+
25+
};
26+
27+
console.log(hammingWeight(11) == 3);
28+
console.log(hammingWeight(0) == 0);
29+
console.log(hammingWeight(1) == 1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy