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

Added Javascript solutions for 9, 110 and 704.#100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
neetcode-gh merged 3 commits intoneetcode-gh:mainfromberkslv:main
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletionsjavascript/110-Balanced-Binary-Tree.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function (root) {
if (!root) return true
const left = findHeight(root.left)
const right = findHeight(root.right)
return Math.abs(left - right) <= 1 && isBalanced(root.left) && isBalanced(root.right)
};

function findHeight(node) {
if (node == null) return 0;
return 1 + Math.max(this.findHeight(node.left), this.findHeight(node.right));
}

// Runtime: 78 ms, faster than 90.43% of JavaScript online submissions for Balanced Binary Tree.
// Memory Usage: 47.1 MB, less than 32.41% of JavaScript online submissions for Balanced Binary Tree.
27 changes: 27 additions & 0 deletionsjavascript/704-Binary-Search.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {

let left = 0;
let right = nums.length - 1;

while (left <= right) {
let middle = Math.floor((left + right) / 2);

if (nums[middle] === target) {
return middle;
} else if (nums[middle] < target) {
left = middle + 1;
} else {
right = middle - 1;
}
}

return -1;
};

// Runtime: 98 ms, faster than 34.02% of JavaScript online submissions for Binary Search.
// Memory Usage: 44.3 MB, less than 99.18% of JavaScript online submissions for Binary Search.
24 changes: 24 additions & 0 deletionsjavascript/9-Palindrome-Number.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {

// Creates array from int characters
// 121 -> [1,2,1]
let arr = Array.from(String(x), Number);

// Uses two pointer
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[arr.length - 1 - i]) {
return false;
}
}

return true;
};


// Runtime: 302 ms, faster than 40.50% of JavaScript online submissions for Palindrome Number.
// Memory Usage: 51.8 MB, less than 8.36% of JavaScript online submissions for Palindrome Number.


[8]ページ先頭

©2009-2025 Movatter.jp