Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63ae02f

Browse files
author
zongyanqi
committed
add Easy_136_Single_Number Easy_190_Reverse_Bits Easy_191_Number_of_1_Bits
1 parentf1cdbbc commit63ae02f

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+
varsingleNumber=function(nums){
22+
23+
returnnums.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+
varreverseBits=function(n){
19+
// reverse binary str
20+
varstr='';
21+
vari=32;
22+
while(i--){
23+
str+=n%2;
24+
n=Math.floor(n/2);
25+
}
26+
27+
returnparseInt(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+
varhammingWeight=function(n){
17+
varweight=0;
18+
19+
while(n>0){
20+
weight+=n%2;
21+
n=Math.floor(n/2);
22+
}
23+
returnweight;
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)

[8]ページ先頭

©2009-2025 Movatter.jp