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

Commitac885d4

Browse files
authored
Merge pull requestneetcode-gh#100 from berkslv/main
Added Javascript solutions for 9, 110 and 704.
2 parentsc4bfa51 +96986ee commitac885d4

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 {boolean}
12+
*/
13+
varisBalanced=function(root){
14+
if(!root)returntrue
15+
constleft=findHeight(root.left)
16+
constright=findHeight(root.right)
17+
returnMath.abs(left-right)<=1&&isBalanced(root.left)&&isBalanced(root.right)
18+
};
19+
20+
functionfindHeight(node){
21+
if(node==null)return0;
22+
return1+Math.max(this.findHeight(node.left),this.findHeight(node.right));
23+
}
24+
25+
// Runtime: 78 ms, faster than 90.43% of JavaScript online submissions for Balanced Binary Tree.
26+
// Memory Usage: 47.1 MB, less than 32.41% of JavaScript online submissions for Balanced Binary Tree.

‎javascript/704-Binary-Search.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} target
4+
*@return {number}
5+
*/
6+
varsearch=function(nums,target){
7+
8+
letleft=0;
9+
letright=nums.length-1;
10+
11+
while(left<=right){
12+
letmiddle=Math.floor((left+right)/2);
13+
14+
if(nums[middle]===target){
15+
returnmiddle;
16+
}elseif(nums[middle]<target){
17+
left=middle+1;
18+
}else{
19+
right=middle-1;
20+
}
21+
}
22+
23+
return-1;
24+
};
25+
26+
// Runtime: 98 ms, faster than 34.02% of JavaScript online submissions for Binary Search.
27+
// Memory Usage: 44.3 MB, less than 99.18% of JavaScript online submissions for Binary Search.

‎javascript/9-Palindrome-Number.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*@param {number} x
3+
*@return {boolean}
4+
*/
5+
varisPalindrome=function(x){
6+
7+
// Creates array from int characters
8+
// 121 -> [1,2,1]
9+
letarr=Array.from(String(x),Number);
10+
11+
// Uses two pointer
12+
for(leti=0;i<arr.length;i++){
13+
if(arr[i]!==arr[arr.length-1-i]){
14+
returnfalse;
15+
}
16+
}
17+
18+
returntrue;
19+
};
20+
21+
22+
// Runtime: 302 ms, faster than 40.50% of JavaScript online submissions for Palindrome Number.
23+
// Memory Usage: 51.8 MB, less than 8.36% of JavaScript online submissions for Palindrome Number.
24+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp