Skip to content

Commit cffc3bd

Browse files
committed
finish 0125
1 parent 49385a8 commit cffc3bd

File tree

8 files changed

+84
-51
lines changed

8 files changed

+84
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ since 2021.10.10 / Benben
3131
- 0053.Maximum SubArray
3232
- 0104.Maximum Depth of Binary Tree
3333
- 0111.Minimum Depth of Binary Tree
34+
- 0125.Valid Palindrome
3435
- 0144.Binary Tree PreOrder Traversal
3536
- 0206.Reverse Linked List
3637
- 0226.Invert Binary Tree

interview/50/04-mergeKSortedLists.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
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[]} lists
10+
* @return {ListNode}
11+
*/
12+
var mergeKLists = function(lists) {
13+
const mergeTwoLists = (l1, l2) => {
14+
if (!l1 || !l2) return l1 || l2
15+
let listNode = new ListNode
16+
let cur = listNode
17+
while(l1 && l2) {
18+
if(l1.val > l2.val) {
19+
cur.next = l2
20+
l2 = l2.next
21+
} else {
22+
cur.next = l1
23+
l1 = l1.next
24+
}
25+
cur = cur.next
26+
}
27+
cur.next = l1 || l2
28+
return listNode.next
29+
}
30+
31+
let root = lists[0];
32+
for (let i = 1; i < lists.length; i++) {
33+
root = mergeTwoLists(root, lists[i]);
34+
}
35+
36+
return root || null;
37+
};
138

239
// leetcode 0023
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
// leetcode 0230

interview/50/09-maxAreaOfIsland.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
// leetcode 695

interview/50/10-reduceString.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
// Input: "CABADCBD"
4+
// Output: ""
5+
// Explanation: "CABADCBD" -> "CADCBD" -> "CABD" -> "CD" -> ""
6+
7+
// Input: "ACBD"
8+
// Output: "ACBD"
9+
// Explanation: "A" 和 "B" 以及 "C" 和 "D" 均沒有相鄰,無法轉態

leetcode-easy/0053-maximum-subarray.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
var maxSubArray = function(nums) {
66
if (nums.length === 0) return nums
77

8-
let cur = nums[0]
9-
let curMax = 0
10-
let res = cur
8+
let cur = 0
9+
let max = -Infinity
1110

12-
for (let i = 1; i < nums.length; i++) {
13-
curMax = Math.max(nums[i], cur + nums[i])
14-
cur = curMax
15-
res = Math.max(res, curMax)
11+
for (let i = 0; i < nums.length; i++) {
12+
cur = Math.max(nums[i], cur + nums[i])
13+
max = Math.max(cur, max)
1614
}
1715

18-
return res
16+
return max
1917
};
2018

19+
2120
// 2022/05/09 done.
22-
// Runtime: 109 ms, faster than 48.46% of JavaScript online submissions for Maximum Subarray.
23-
// Memory Usage: 50.4 MB, less than 48.90% of JavaScript online submissions for Maximum Subarray.
21+
// Runtime: 95 ms, faster than 68.36% of JavaScript online submissions for Maximum Subarray.
22+
// Memory Usage: 50.2 MB, less than 73.28% of JavaScript online submissions for Maximum Subarray.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function(s) {
6+
let str = s
7+
.toLowerCase()
8+
.replace(/[^a-z0-9]/g, '')
9+
10+
let left = 0
11+
let right = str.length - 1
12+
while (left <= right) {
13+
if (str[left] !== str[right]) {
14+
return false
15+
}
16+
left++
17+
right--
18+
}
19+
return true
20+
};
21+
22+
// 2022/05/11 done.
23+
// Runtime: 110 ms, faster than 36.67% of JavaScript online submissions for Valid Palindrome.
24+
// Memory Usage: 44.2 MB, less than 82.99% of JavaScript online submissions for Valid Palindrome.

leetcode-hard/0023-.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)