Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Week_01/id_39/LeetCode_101_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
* https://leetcode.com/problems/symmetric-tree/
*/

// 递归法
var treeSymmetric = function(t1, t2) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t1, t2,最好能写成left, right, 容易懂些。

if (t1 == null || t2 == null) {
return t1 == t2;
}
if (t1.val !== t2.val) {
return false;
}
return treeSymmetric(t1.left, t2.right) && treeSymmetric(t1.right, t2.left);
};
var isSymmetric = function(root) {
if (!root) {
return true;
}
return treeSymmetric(root.left, root.right);
};


// 迭代法
21 changes: 21 additions & 0 deletions Week_01/id_39/LeetCode_1021_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string} S
* @return {string}
* https://leetcode.com/problems/remove-outermost-parentheses/
* 标志位,刨除标志位为0的时候,其余时候都移到结果中
*/
var removeOuterParentheses = function(S) {
let sign = 0;
let result = '';
let i = 0;
while (i < S.length) {
let cur = S.charAt(i);
if (cur === '(' && sign++ > 0) {
result += cur;
} else if (cur === ')' && sign-- > 1) {
result += cur;
}
i++;
}
return result;
};
21 changes: 21 additions & 0 deletions Week_01/id_39/LeetCode_1047_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string} S
* @return {string}
* https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
*/
var removeDuplicates = function(S) {
let arr = S.split('');
let stack = [];
for (let j = 0; j < arr.length; j++) {
if (!stack.length) {
stack.push(arr[j]);
continue;
}
let prev = stack.pop();
if (arr[j] !== prev) {
stack.push(prev);
stack.push(arr[j]);
}
}
return stack.join('');
};
55 changes: 55 additions & 0 deletions Week_01/id_39/LeetCode_104_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
* https://leetcode.com/problems/maximum-depth-of-binary-tree/
*/
// 广度优先搜索
var maxDepth = function(root) {
if (!root) {
return 0;
}

let queue = [];
let level = [root];
let depth = 0;

while (level.length) {
depth++;

while (level.length) {
let node = level.shift();
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
level = queue;
queue = [];
}

return depth;
};

// DFS 深度优先搜索 递归形式
var maxDepth = function(root) {
if (!root) {
return 0;
}
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
};

// DFS 深度优先搜索 非递归形式
var maxDepth = function(root) {
if (!root) {
return 0;
}
};
25 changes: 25 additions & 0 deletions Week_01/id_39/LeetCode_111_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
* https://leetcode.com/problems/minimum-depth-of-binary-tree/
* 注意:最小深度是根节点到叶子节点,so 如果只有根节点和它都某一子节点,则深度为2
*/

var minDepth = function(root) {
if (!root) {
return 0;
}
let left = minDepth(root.left);
let right = minDepth(root.right);
if (left == 0 || right == 0) {
return left + right + 1;
}
return Math.min(left, right) + 1;
};
56 changes: 56 additions & 0 deletions Week_01/id_39/LeetCode_141_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
* https://leetcode.com/problems/maximum-depth-of-binary-tree/
*/
// 广度优先搜索
var maxDepth = function(root) {
if (!root) {
return 0;
}

let queue = [];
let level = [root];
let depth = 0;

while (level.length) {
depth++;

while (level.length) {
let node = level.shift();
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
level = queue;
queue = [];
}

return depth;
};

// DFS 深度优先搜索 递归形式
var maxDepth = function(root) {
if (!root) {
return 0;
}
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
};

// DFS 深度优先搜索 非递归形式
var maxDepth = function(root) {
if (!root) {
return 0;
}
//
};
52 changes: 52 additions & 0 deletions Week_01/id_39/LeetCode_15_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @param {number[]} nums
* @return {number[][]}
* https://leetcode.com/problems/3sum/
* 备注:这一版太乱了, 内存性能都非常差!!!
*/

var threeSum = function(arrs) {
if (arrs.length < 3) {
return [];
}
let nums = [];
let map = new Map();
for (let i = 0; i < arrs.length; i++) {
let cur = arrs[i];
if (map.has(cur)) {
let count = map.get(cur);
if (count < 3) {
map.set(cur, count + 1);
nums.push(cur);
}
} else {
map.set(cur, 1);
nums.push(cur);
}
}
// nums = temps;
let result = new Set();
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length; j++) {
let a = nums[i];
let b = nums[j];
let c = (-1) * (a + b);
if (map.has(c)) {
if ((a == b && b == c && c == a) && map.get(a) < 3) {
continue;
}
if ((a == b || a == c) && map.get(a) < 2) {
continue;
}
if (b == c && map.get(b) < 2) {
continue;
}
let arr = [a, b, c].sort().join(',');
result.add(arr);
}
}
}
return Array.from(result, (val) => {
return val.split(',')
});
};
26 changes: 26 additions & 0 deletions Week_01/id_39/LeetCode_189_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
* https://leetcode.com/problems/rotate-array/
* 思路:比较难想,3次reverse
* tips: 3种不同思路
*/
var reverse = function(nums, i, j) {
while(i < j) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp
i++;
j--;
}
}

var rotate = function(nums, k) {
let len = nums.length;
k %= len;

reverse(nums, 0, len - k - 1);
reverse(nums, len - k, len - 1);
reverse(nums, 0, len - 1);
};
43 changes: 43 additions & 0 deletions Week_01/id_39/LeetCode_21_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
* https://leetcode.com/problems/merge-two-sorted-lists/
* 思路:新建一个链表,头指针是一个哨兵位,遍历两个链表,比较大小,小的在前,并指针移到下一位
*/

function ListNode(val) {
this.val = val;
this.next = null;
}

var mergeTwoLists = function(l1, l2) {
if (!l1 && !l2) {
return null;
}
let head = new ListNode(null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了您的代码,更深刻地理解了哨兵的说法

let cur1 = l1;
let cur2 = l2;
let cur = head;
while(cur1 || cur2) {
if ((cur1 && !cur2) || (cur1 && cur2 && (cur2.val > cur1.val))) {
cur.next = new ListNode(cur1.val);
cur = cur.next;
cur1 = cur1.next;
continue;
} else {
cur.next = new ListNode(cur2.val);
cur = cur.next;
cur2 = cur2.next;
continue;
}
}
return head.next;
};
25 changes: 25 additions & 0 deletions Week_01/id_39/LeetCode_236_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
*/

// 分别在左右子树查找p或者q,找到了就返回
var lowestCommonAncestor = function(root, p, q) {
if (!root || root == p || root == q) {
return root;
}
let left = lowestCommonAncestor(root.left, p, q);
let right = lowestCommonAncestor(root.right, p, q);

return left == null ? right : right == null ? left : root;
};
Loading