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

Commitcfd4c0e

Browse files
committed
add 247, 326, 242 bit manipulations
1 parent8508bc4 commitcfd4c0e

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

‎Easy/326-powerOfThree.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*@param {number} n
3+
*@return {boolean}
4+
*/
5+
// recursive
6+
varisPowerOfThree=function(n){
7+
if(n===0)returnfalse;
8+
if(n===1)returntrue;
9+
returnn%3===0&&isPowerOfThree(Math.floor(n/3));
10+
};
11+
12+
// iterative Ways
13+
varisPowerOfThree=function(n){
14+
if(n>1){
15+
while(n%3===0){
16+
n=Math.floor(n/3);
17+
}
18+
}
19+
returnn===1;
20+
};

‎Easy/342-powerOfFour.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*@param {number} num
3+
*@return {boolean}
4+
*/
5+
varisPowerOfFour=function(num){
6+
if(num>1){
7+
while(num%4===0){
8+
num=Math.floor(num/4);
9+
}
10+
}
11+
returnnum===1;
12+
};
13+
14+
varisPowerOfFour=function(num){
15+
if(num>1){
16+
if((num&(num-1))===0){
17+
if((num-1)%3===0){
18+
returntrue;
19+
}
20+
}
21+
}
22+
returnnum===1;
23+
};
24+
25+
// the '1' bit only appears at odd bit, so if any every 3 bits (=5) has 1, it is
26+
// a number of power of 4
27+
varisPowerOfFour=function(num){
28+
if(num>1){
29+
if((num&(num-1))===0){
30+
if((num&0x55555555)!==0){
31+
returntrue;
32+
}
33+
}
34+
}
35+
returnnum===1;
36+
};

‎Medium/247-topKFrequentElements.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Solution: 1. priority queue, O(n*log(k)), however JavaScript doesn't natively support heap
3+
* 2. bucket sort
4+
*
5+
*@param {number[]} nums
6+
*@param {number} k
7+
*@return {number[]}
8+
*/
9+
vartopKFrequent=function(nums,k){
10+
varmap={};
11+
for(vari=0;i<nums.length;i++){
12+
if(map.hasOwnProperty(nums[i])){
13+
map[nums[i]]+=1;
14+
}else{
15+
map[nums[i]]=1;
16+
}
17+
}
18+
19+
varbucket=[];
20+
for(varkeyinmap){
21+
varkeyVal=parseInt(key,10);
22+
bucket[map[key]] ?bucket[map[key]].push(keyVal) :bucket[map[key]]=[keyVal];
23+
}
24+
25+
varresult=[];
26+
27+
for(varj=bucket.length-1;j>0;j--){
28+
if(bucket[j]){
29+
result=result.concat(bucket[j]);
30+
}
31+
32+
if(result.length>=k)break;
33+
}
34+
35+
returnresult;
36+
37+
};

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@
6565
*[290. Word Pattern](https://leetcode.com/problems/word-pattern/) -[Solution](./Easy/290-wordPattern.js)
6666
*[299. Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) -[Solution](./Easy/299-bullsandCows.js)
6767
*[303. Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) -[Solution](./Easy/303-rangeSumQuery.js)
68+
*[326. Power of Three](https://leetcode.com/problems/power-of-three/) -[Solution](./Easy/326-powerOfThree.js)
6869
*[328. Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) -[Solution](./Easy/328-oddevenLinkedList.js)
70+
*[342. Power of Four](https://leetcode.com/problems/power-of-four/) -[Solution](./Easy/342-powerOfFour.js)
71+
6972

7073
#####Medium
7174
*[1. Two Sum](https://leetcode.com/problems/two-sum/) -[Solution](./Medium/1-twoSum.js)
@@ -164,6 +167,7 @@
164167
*[318. Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) -[Solution](./Medium/318-maximumProductWordLengths.js)
165168
*[319. Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) -[Solution](./Medium/319-bulbSwitcher.js)
166169
*[337. House Robber III](https://leetcode.com/problems/house-robber-iii/) -[Solution](./Medium/337-houseRobberIII.js)
170+
*[347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) -[Solution](./Medium/247-topKFrequentElements.js)
167171

168172
#####Hard
169173
*[23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) -[Solution](./Hard/23-MergeKSortedLists.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp