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

Commit64faa2e

Browse files
committed
add 108, 141, 318, 35, 96
1 parent6afe52e commit64faa2e

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
*@param {number[]} nums
10+
*@return {TreeNode}
11+
*/
12+
varsortedArrayToBST=function(nums){
13+
varhigh=nums.length-1;
14+
returnarrayToBSTHelper(nums,0,high);
15+
};
16+
17+
vararrayToBSTHelper=function(nums,low,high){
18+
if(low>high)returnnull;
19+
varmid=low+Math.floor((high-low)/2);
20+
varroot=newTreeNode(nums[mid]);
21+
root.left=arrayToBSTHelper(nums,low,mid-1);
22+
root.right=arrayToBSTHelper(nums,mid+1,high);
23+
returnroot;
24+
};

‎Medium/141-linkedListCycle.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* Key: two pointers, slow move one step one time, fast moves two steps one time
11+
* if there is a cycle fast will meet slow
12+
*
13+
*@param {ListNode} head
14+
*@return {boolean}
15+
*/
16+
varhasCycle=function(head){
17+
if(!head)returnfalse;
18+
varfast=head;
19+
varslow=head;
20+
while(fast.next&&fast.next.next){
21+
slow=slow.next;
22+
fast=fast.next.next
23+
if(slow===fast)returntrue;
24+
}
25+
26+
returnfalse;
27+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Key: Bit manipulation. 26 letters, from bit 0 to 25, each bit represent a letter,
3+
* so each word can be represented by an 'or |' operation of all bits. The two words
4+
* share no common letter only if the represented bits of the two words do not share
5+
* a common bit. (use & to solve this problem).
6+
*
7+
*@param {string[]} words
8+
*@return {number}
9+
*/
10+
varmaxProduct=function(words){
11+
varbytes=[];
12+
varmaxLength=0;
13+
for(vari=0;i<words.length;i++){
14+
varbit=0;
15+
for(varj=0;j<words[i].length;j++){
16+
bit|=1<<(words[i].charCodeAt(j)-'a'.charCodeAt(0));
17+
}
18+
bytes[i]=bit;
19+
}
20+
21+
for(vari=0;i<words.length;i++){
22+
for(varj=i+1;j<words.length;j++){
23+
// the operator priority '===' > '&', so we need () for the & operator
24+
if((bytes[j]&bytes[i])===0){
25+
maxLength=Math.max(maxLength,words[i].length*words[j].length);
26+
}
27+
}
28+
}
29+
30+
returnmaxLength;
31+
};

‎Medium/35-searchInsertPosition.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} target
4+
*@return {number}
5+
*/
6+
// it not O(logN)
7+
varsearchInsert=function(nums,target){
8+
for(vari=0;i<nums.length;i++){
9+
if(nums[i]>=target)returni;
10+
}
11+
12+
returnnums.length;
13+
};
14+
15+
// because it is a sorted array, we can use binary search.
16+
varsearchInsert=function(nums,target){
17+
varlow=0;
18+
varhigh=nums.length-1;
19+
20+
while(low<=high){
21+
varmid=low+Math.floor((high-low)/2);
22+
if(nums[mid]===target)returnmid;
23+
if(target<nums[mid])high=mid-1;
24+
elselow=mid+1;
25+
}
26+
27+
returnlow;
28+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Key: Dynamic Programming.
3+
* Binary search tree: all left nodes are less than root, and all right node are greater than root
4+
* That means, given number n, if the root is i, only numbers between [0, i-1] appear on the left, while
5+
* only numbers between [i+1, n] appear on the right. Thus, the total combination is m*n.
6+
* Let count[i] be the total possible BST structure ways when the node number is i.
7+
* count[i] = sum(count[k], count[i- k - 1]), k starts from 0 to i-1
8+
* e.g. count[3] = count[0] * count[2] // when root is 1;
9+
* + count[1] * count[1] // when root is 2;
10+
* + count[2] * count[0] // when root is 3;
11+
*
12+
*@param {number} n
13+
*@return {number}
14+
*/
15+
varnumTrees=function(n){
16+
varcount=[1,1];
17+
18+
for(vari=2;i<=n;i++){
19+
// give an intial value to count[i], otherwise, it will be NaN
20+
count[i]=0;
21+
for(varj=0;j<i;j++){
22+
count[i]+=count[j]*count[i-j-1];
23+
}
24+
}
25+
26+
returncount[n];
27+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp