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

Commit a494dfc

Browse files
feat: update 5 leetcode
1 parent 36b4f6f commit a494dfc

6 files changed

+247
-1
lines changed

‎collection.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getChain(arr) {
2323
map.set(key, value);
2424
}
2525
for (let i = 0; i < arr.length; i++) {
26-
const [key,value] = arr[i];
26+
const [key] = arr[i];
2727
let first = key;
2828
let current = [];
2929

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @lc app=leetcode id=109 lang=javascript
3+
*
4+
* [109] Convert Sorted List to Binary Search Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* Definition for a binary tree node.
17+
* function TreeNode(val, left, right) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.left = (left===undefined ? null : left)
20+
* this.right = (right===undefined ? null : right)
21+
* }
22+
*/
23+
/**
24+
* 分治的思路
25+
* @param {ListNode} head
26+
* @return {TreeNode}
27+
*/
28+
var sortedListToBST = function(head) {
29+
/**
30+
* 使用双指针快速找到中间节点
31+
* @param {*} left
32+
* @param {*} right
33+
* @returns
34+
*/
35+
function getMid(left, right) {
36+
let fast = left;
37+
let slow = left;
38+
39+
while (fast !== right && fast.next !== right) {
40+
fast = fast.next;
41+
fast = fast.next;
42+
slow = slow.next;
43+
}
44+
45+
return slow;
46+
}
47+
48+
/**
49+
* 前序遍历的方式递归找到子节点并赋值
50+
* @param {*} left
51+
* @param {*} right
52+
* @returns
53+
*/
54+
function buildTree(left, right) {
55+
if (left === right) {
56+
return null;
57+
}
58+
const mid = getMid(left, right);
59+
let root = new TreeNode(mid.val);
60+
root.left = buildTree(left, mid);
61+
root.right = buildTree(mid.next, right);
62+
return root;
63+
}
64+
65+
return buildTree(head, null);
66+
};
67+
// @lc code=end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @lc app=leetcode id=234 lang=javascript
3+
*
4+
* [234] Palindrome Linked List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* 递归的方式,利用堆栈的特性来实现
17+
* 递归中的链表和递归外的链表
18+
* 时间复杂度和空间复杂度都是 O(n)
19+
* @param {ListNode} head
20+
* @return {boolean}
21+
*/
22+
var isPalindrome = function(head) {
23+
let prev = head;
24+
25+
/**
26+
* 利用堆栈的特性,因而递归函数中取到的 node 是从末尾开始的
27+
* 因此可以将末尾的和当前的进行比较
28+
* @param {*} node
29+
* @returns
30+
*/
31+
function recursivelyCheck(node) {
32+
if (node !== null) {
33+
if (!recursivelyCheck(node.next)) {
34+
return false;
35+
}
36+
if (node.val !== prev.val) {
37+
return false;
38+
}
39+
prev = prev.next;
40+
}
41+
return true;
42+
}
43+
44+
return recursivelyCheck(head);
45+
};
46+
47+
/**
48+
* 将链表转成数组,再使用双指针首尾进行比较
49+
* 时间复杂度和空间复杂度都是 O(n)
50+
* @param {ListNode} head
51+
* @return {boolean}
52+
*/
53+
var isPalindrome2 = function(head) {
54+
let list = [];
55+
56+
while (head) {
57+
list.push(head.val);
58+
head = head.next;
59+
}
60+
61+
let firstIndex = 0;
62+
let lastIndex = list.length - 1;
63+
while (firstIndex <= lastIndex) {
64+
if (list[firstIndex] !== list[lastIndex]) {
65+
return false;
66+
}
67+
firstIndex++;
68+
lastIndex--;
69+
}
70+
return true;
71+
};
72+
// @lc code=end

‎leetcode/498.diagonal-traverse.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @lc app=leetcode id=498 lang=javascript
3+
*
4+
* [498] Diagonal Traverse
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[][]} mat
10+
* @return {number[]}
11+
*/
12+
var findDiagonalOrder = function(mat) {
13+
let row = 0; // 当前遍历的行
14+
let totalCol = 0; // 保存奇数还是偶数,也就是第几行
15+
let col = 0; // 当前遍历的列
16+
let ans = [];
17+
const maxColLength = mat[0].length;
18+
const maxRowLength = mat.length;
19+
const totalCount = maxColLength * maxRowLength;
20+
21+
while (ans.length < totalCount) {
22+
// 如果超出了当前列的总数,则重置为最后一位
23+
col = totalCol > maxColLength - 1 ? maxColLength - 1 : totalCol;
24+
// 如果第一行已经遍历完,就往后依次叠加
25+
row = totalCol > maxColLength - 1 ? totalCol - (maxColLength - 1) : 0;
26+
let res = [];
27+
28+
// 折线遍历内容
29+
while (col >= 0 && row < maxRowLength) {
30+
const current = mat[row][col];
31+
res.push(current);
32+
33+
col--;
34+
row++;
35+
}
36+
// 判断奇数偶数来确认数组的推入方向
37+
ans = ans.concat(totalCol % 2 === 0 ? res.reverse() : res);
38+
totalCol++;
39+
}
40+
41+
return ans;
42+
};
43+
// @lc code=end

‎leetcode/53.maximum-subarray.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode id=53 lang=javascript
3+
*
4+
* [53] Maximum Subarray
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 下面是动态规划的思路
10+
* 也可以使用分治思路解决
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var maxSubArray = function(nums) {
15+
let pre = Number.MIN_SAFE_INTEGER;
16+
let maxVal = nums[0];
17+
18+
// 整体思路有些类似于爬楼梯
19+
nums.forEach((x) => {
20+
// 累加值和当前值比较来取出较大的值
21+
pre = Math.max(pre + x, x);
22+
// 当前最大值和已保存的最大值比较取出较大值
23+
maxVal = Math.max(pre, maxVal);
24+
});
25+
26+
return maxVal;
27+
};
28+
// @lc code=end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=98 lang=javascript
3+
*
4+
* [98] Validate Binary Search Tree
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {TreeNode} root
18+
* @return {boolean}
19+
*/
20+
var isValidBST = function(root) {
21+
function dfs(node, left, right) {
22+
if (node === null) {
23+
return true;
24+
}
25+
// 判断大小是否异常
26+
if (node.val >= right || node.val <= left) {
27+
return false;
28+
}
29+
30+
// 根据左值始终比当前节点小,而右值始终比当前节点大的规律来递归
31+
return dfs(node.left, left, node.val) && dfs(node.right, node.val, right);
32+
}
33+
34+
return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
35+
};
36+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /