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

Commit12654ef

Browse files
authored
Merge pull requestneetcode-gh#3 from veer-p/main
JavaScript Solutions for 1, 11, 15, 19, 20, 21, 33, 48, 49, 53, 54, 70, 73, 98, 100, 104, 121, 125, 141, 143, 152, 153, 206, 217, 226, 238, 242, 268, 300, 572
2 parents0013be3 +42d81ba commit12654ef

File tree

29 files changed

+734
-0
lines changed

29 files changed

+734
-0
lines changed

‎javascript/1-Two-Sum.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} target
4+
*@return {number[]}
5+
*/
6+
vartwoSum=function(nums,target){
7+
letmap={};
8+
for(leti=0;i<nums.length;i++){
9+
if(target-nums[i]inmap){
10+
return[map[target-nums[i]],i];
11+
}else{
12+
map[nums[i]]=i;
13+
}
14+
}
15+
};

‎javascript/100-Same-Tree.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
*@param {TreeNode} p
11+
*@param {TreeNode} q
12+
*@return {boolean}
13+
*/
14+
varisSameTree=function(p,q){
15+
if(!p&&!q)returntrue;
16+
if(!p||!q||p.val!==q.val)returnfalse;
17+
returnisSameTree(p.right,q.right)&&isSameTree(p.left,q.left);
18+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
*@param {TreeNode} root
11+
*@return {number}
12+
*/
13+
varmaxDepth=(root)=>{
14+
letmaxDepth=0;
15+
letDFS=(node,depth)=>{
16+
if(!node)returnmaxDepth;
17+
if(depth>maxDepth)maxDepth=depth;
18+
DFS(node.right,depth+1);
19+
DFS(node.left,depth+1);
20+
}
21+
DFS(root,1);
22+
returnmaxDepth;
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*@param {number[]} height
3+
*@return {number}
4+
*/
5+
varmaxArea=function(height){
6+
letmax=0;
7+
leti=0;
8+
letj=height.length-1;
9+
10+
while(i<j){
11+
curr=(j-i)*Math.min(height[i],height[j]);
12+
max=Math.max(curr,max);
13+
if(height[i]>height[j]){
14+
j--;
15+
}else{
16+
i++;
17+
}
18+
}
19+
returnmax;
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
*@param {number[]} prices
3+
*@return {number}
4+
*/
5+
varmaxProfit=function(prices){
6+
letbuy=prices[0];
7+
letprofit=0;
8+
for(leti=0;i<prices.length;i++){
9+
if(prices[i]<buy){
10+
buy=prices[i];
11+
}else{
12+
profit=Math.max(prices[i]-buy,profit);
13+
}
14+
}
15+
returnprofit;
16+
};

‎javascript/125-Valid-Palindrome.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*@param {string} s
3+
*@return {boolean}
4+
*/
5+
varisPalindrome=function(s){
6+
s=s.toLowerCase();
7+
s=s.replace(/[^A-Z0-9]/gi,'');
8+
9+
leti=0
10+
letj=s.length-1;
11+
12+
while(i<j){
13+
if(s[i]!==s[j])returnfalse;
14+
15+
i++;
16+
j--
17+
}
18+
returntrue;
19+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
*@param {ListNode} head
11+
*@return {boolean}
12+
*/
13+
varhasCycle=function(head){
14+
letset=newSet();
15+
while(head){
16+
if(set.has(head)){
17+
returntrue;
18+
}else{
19+
set.add(head);
20+
head=head.next;
21+
}
22+
}
23+
24+
returnfalse;
25+
};

‎javascript/143-Reorder-List.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
varreorderList=function(head){
13+
if(!head){return};
14+
15+
letslow=head;
16+
letfast=head;
17+
18+
19+
// finding the middle of the linked list using 2 pters
20+
while(fast&&fast.next){
21+
slow=slow.next;
22+
fast=fast.next.next;
23+
}
24+
25+
// reverse the second part of the list starting at slow
26+
letprev=null
27+
letcurr=slow;
28+
while(curr){
29+
letnext=curr.next;
30+
curr.next=prev;
31+
prev=curr;
32+
curr=next;
33+
}// here prev is the head
34+
35+
// merge two sorted lists (first one starts at head, second at prev)
36+
letfirst=head;
37+
letsecond=prev;
38+
39+
while(second.next){
40+
temp=first.next;
41+
first.next=second;
42+
first=temp;
43+
44+
temp=second.next;
45+
second.next=first;
46+
second=temp;
47+
}
48+
49+
50+
};

‎javascript/15-3Sum.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {number[][]}
4+
*/
5+
varthreeSum=function(nums){
6+
letres=[];
7+
letleft=0;
8+
letright=nums.length-1;
9+
nums.sort((a,b)=>{returna-b})
10+
11+
for(leti=0;i<nums.length-1;i++){
12+
if(nums[i]>0)returnres;
13+
if(nums[i]===nums[i-1])continue;
14+
15+
left=i+1;
16+
right=nums.length-1;
17+
lettemp=0;
18+
19+
while(left<right){
20+
temp=nums[left]+nums[right]+nums[i];
21+
if(temp===0){
22+
res.push([nums[i],nums[left],nums[right]]);
23+
left++;
24+
right--;
25+
26+
while(nums[left]==nums[left-1]){left++};
27+
28+
while(nums[right]==nums[right-1]){right--};
29+
30+
}elseif(temp>0){right--}
31+
elseif(temp<0){left++}
32+
}
33+
}
34+
returnres;
35+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*@param {number[]} nums
3+
*@return {number}
4+
*/
5+
varmaxProduct=function(nums){
6+
letresult=nums[0];
7+
letprevMax=nums[0];
8+
letprevMin=nums[0];
9+
for(leti=1;i<nums.length;i++){
10+
currMax=Math.max(nums[i],prevMax*nums[i],prevMin*nums[i]);
11+
currMin=Math.min(nums[i],prevMax*nums[i],prevMin*nums[i]);
12+
13+
prevMax=currMax;
14+
prevMin=currMin;
15+
16+
result=Math.max(currMax,result);
17+
}
18+
returnresult;
19+
20+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp