-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
667f5eb
commit 753a605
Showing
9 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var searchInsert = function(nums, target) { | ||
let left = 0 | ||
let right = nums.length - 1 | ||
let mid = 0 | ||
|
||
if (target <= nums[left]) return 0 | ||
if (target > nums[right]) return nums.length | ||
if (target === nums[right]) return nums.length - 1 | ||
|
||
while (left <= right) { | ||
mid = Math.floor((left + right) / 2) | ||
|
||
if (target === nums[mid]) return mid | ||
|
||
if (target > nums[mid]) { | ||
left = mid + 1 | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
if (target > nums[mid] && target < nums[mid + 1]) return mid + 1 | ||
return mid | ||
}; | ||
|
||
|
||
// leetcode 0035 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} left | ||
* @param {number} right | ||
* @return {ListNode} | ||
*/ | ||
var reverseBetween = function(head, left, right) { | ||
let successor = null | ||
|
||
const reverseN = (head, n) => { | ||
if (n === 1) { | ||
successor = head.next | ||
return head | ||
} | ||
const last = reverseN(head.next, n - 1) | ||
head.next.next = head | ||
head.next = successor | ||
return last | ||
} | ||
|
||
const doReverse = (head, left, right) => { | ||
if (left === 1) { | ||
return reverseN(head, right) | ||
} | ||
head.next = doReverse(head.next, left - 1, right - 1) | ||
return head | ||
} | ||
return doReverse(head, left, right) | ||
}; | ||
|
||
// leetcode 0092 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 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 {number} | ||
*/ | ||
var diameterOfBinaryTree = function(root) { | ||
let maxDiameter = 0 | ||
|
||
const traverse = (root) => { | ||
if (!root) return 0 | ||
|
||
let leftLength = traverse(root.left) | ||
let rightLength = traverse(root.right) | ||
|
||
maxDiameter = Math.max(maxDiameter, leftLength + rightLength) | ||
|
||
return 1 + Math.max(leftLength, rightLength) | ||
} | ||
traverse(root) | ||
|
||
return maxDiameter | ||
}; | ||
|
||
// leetcode 0543 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
// leetcode 0023 |
2 changes: 2 additions & 0 deletions
2
interview/50/05-removeZeroSumConsecutiveNodesFromLinkedList.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
// leetcode 1171 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var maxSubArray = function(nums) { | ||
if (nums.length === 0) return nums | ||
|
||
let cur = nums[0] | ||
let curMax = 0 | ||
let res = cur | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
curMax = Math.max(nums[i], cur + nums[i]) | ||
cur = curMax | ||
res = Math.max(res, curMax) | ||
} | ||
|
||
return res | ||
} | ||
|
||
// leetcode 0053 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters