Skip to content

Commit

Permalink
finish 0125
Browse files Browse the repository at this point in the history
  • Loading branch information
benben6515 committed May 11, 2022
1 parent 49385a8 commit cffc3bd
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ since 2021.10.10 / Benben
- 0053.Maximum SubArray
- 0104.Maximum Depth of Binary Tree
- 0111.Minimum Depth of Binary Tree
- 0125.Valid Palindrome
- 0144.Binary Tree PreOrder Traversal
- 0206.Reverse Linked List
- 0226.Invert Binary Tree
Expand Down
37 changes: 37 additions & 0 deletions interview/50/04-mergeKSortedLists.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
const mergeTwoLists = (l1, l2) => {
if (!l1 || !l2) return l1 || l2
let listNode = new ListNode
let cur = listNode
while(l1 && l2) {
if(l1.val > l2.val) {
cur.next = l2
l2 = l2.next
} else {
cur.next = l1
l1 = l1.next
}
cur = cur.next
}
cur.next = l1 || l2
return listNode.next
}

let root = lists[0];
for (let i = 1; i < lists.length; i++) {
root = mergeTwoLists(root, lists[i]);
}

return root || null;
};

// leetcode 0023
2 changes: 2 additions & 0 deletions interview/50/08-kthSmallestElementInBST.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// leetcode 0230
2 changes: 2 additions & 0 deletions interview/50/09-maxAreaOfIsland.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// leetcode 695
9 changes: 9 additions & 0 deletions interview/50/10-reduceString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


// Input: "CABADCBD"
// Output: ""
// Explanation: "CABADCBD" -> "CADCBD" -> "CABD" -> "CD" -> ""

// Input: "ACBD"
// Output: "ACBD"
// Explanation: "A" 和 "B" 以及 "C" 和 "D" 均沒有相鄰,無法轉態
19 changes: 9 additions & 10 deletions leetcode-easy/0053-maximum-subarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
var maxSubArray = function(nums) {
if (nums.length === 0) return nums

let cur = nums[0]
let curMax = 0
let res = cur
let cur = 0
let max = -Infinity

for (let i = 1; i < nums.length; i++) {
curMax = Math.max(nums[i], cur + nums[i])
cur = curMax
res = Math.max(res, curMax)
for (let i = 0; i < nums.length; i++) {
cur = Math.max(nums[i], cur + nums[i])
max = Math.max(cur, max)
}

return res
return max
};


// 2022/05/09 done.
// Runtime: 109 ms, faster than 48.46% of JavaScript online submissions for Maximum Subarray.
// Memory Usage: 50.4 MB, less than 48.90% of JavaScript online submissions for Maximum Subarray.
// Runtime: 95 ms, faster than 68.36% of JavaScript online submissions for Maximum Subarray.
// Memory Usage: 50.2 MB, less than 73.28% of JavaScript online submissions for Maximum Subarray.
24 changes: 24 additions & 0 deletions leetcode-easy/0125-valid-palindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
let str = s
.toLowerCase()
.replace(/[^a-z0-9]/g, '')

let left = 0
let right = str.length - 1
while (left <= right) {
if (str[left] !== str[right]) {
return false
}
left++
right--
}
return true
};

// 2022/05/11 done.
// Runtime: 110 ms, faster than 36.67% of JavaScript online submissions for Valid Palindrome.
// Memory Usage: 44.2 MB, less than 82.99% of JavaScript online submissions for Valid Palindrome.
41 changes: 0 additions & 41 deletions leetcode-hard/0023-.js

This file was deleted.

0 comments on commit cffc3bd

Please sign in to comment.