Skip to content

Commit 230c8a0

Browse files
committed
4 solutions
1 parent 738de32 commit 230c8a0

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

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+
var findMin = function(nums) {
6+
let left = 0;
7+
let right = nums.length - 1;
8+
9+
while (left < right) {
10+
const mid = Math.floor((left + right) / 2);
11+
12+
if (nums[mid] > nums[right]) {
13+
left = mid + 1;
14+
} else {
15+
right = mid;
16+
}
17+
}
18+
19+
return nums[left];
20+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
var maxDepth = function(root) {
14+
if (root === null) return 0;
15+
16+
const left = maxDepth(root.left);
17+
const right = maxDepth(root.right);
18+
19+
return Math.max(left, right) + 1;
20+
};

merge-two-sorted-lists/nowrobin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function(list1, list2) {
14+
let dummy = new ListNode(0);
15+
let current = dummy;
16+
while (list1 !== null && list2 !== null) {
17+
if (list1.val < list2.val) {
18+
current.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
current.next = list2;
22+
list2 = list2.next;
23+
}
24+
current = current.next;
25+
}
26+
current.next = list1 !== null ? list1 : list2;
27+
28+
return dummy.next;
29+
};

word-search/nowrobin.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
var exist = function(board, word) {
7+
const rows = board.length;
8+
const cols = board[0].length;
9+
10+
function dfs(r, c, index) {
11+
if (r < 0 || c < 0 || r >= rows || c >= cols) return false;
12+
if (board[r][c] !== word[index]) return false;
13+
14+
if (index === word.length - 1) return true;
15+
16+
const temp = board[r][c];
17+
board[r][c] = "#";
18+
19+
const found =
20+
dfs(r + 1, c, index + 1) ||
21+
dfs(r - 1, c, index + 1) ||
22+
dfs(r, c + 1, index + 1) ||
23+
dfs(r, c - 1, index + 1);
24+
25+
board[r][c] = temp;
26+
27+
return found;
28+
}
29+
30+
for (let r = 0; r < rows; r++) {
31+
for (let c = 0; c < cols; c++) {
32+
if (dfs(r, c, 0)) return true;
33+
}
34+
}
35+
36+
return false;
37+
};

0 commit comments

Comments
 (0)