diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index 97f51e7f..98df800a 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -288,7 +288,34 @@ class Solution: return s[left:right + 1] ``` +> 双指针法: +```python +class Solution: + def longestPalindrome(self, s: str) -> str: + def find_point(i, j, s): + while i >= 0 and j < len(s) and s[i] == s[j]: + i -= 1 + j += 1 + return i + 1, j + + def compare(start, end, left, right): + if right - left > end - start: + return left, right + else: + return start, end + + start = 0 + end = 0 + for i in range(len(s)): + left, right = find_point(i, i, s) + start, end = compare(start, end, left, right) + + left, right = find_point(i, i + 1, s) + start, end = compare(start, end, left, right) + return s[start:end] + +``` ## Go ```go diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index df1790c3..da4398f5 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -421,9 +421,10 @@ var levelOrderBottom = function(root) { node.left&&queue.push(node.left); node.right&&queue.push(node.right); } - res.push(curLevel); + // 从数组前头插入值,避免最后反转数组,减少运算时间 + res.unshift(curLevel); } - return res.reverse(); + return res; }; ``` @@ -1263,7 +1264,41 @@ class Solution: first = first.left # 从本层扩展到下一层 return root ``` +JavaScript: +```javascript +/** + * // Definition for a Node. + * function Node(val, left, right, next) { + * this.val = val === undefined ? null : val; + * this.left = left === undefined ? null : left; + * this.right = right === undefined ? null : right; + * this.next = next === undefined ? null : next; + * }; + */ + +/** + * @param {Node} root + * @return {Node} + */ +var connect = function(root) { + if (root === null) return root; + let queue = [root]; + while (queue.length) { + let n = queue.length; + for (let i=0; i 0) { + let n = queue.length; + for (let i=0; i 1) { + copy(); + } + int* uset = (int*)malloc(sizeof(int) * numsSize); + int usetTop = 0; + int i; + for(i = startIndex; i < numsSize; i++) { + //若当前元素小于path中最后一位元素 || 在树的同一层找到了相同的元素,则continue + if((pathTop > 0 && nums[i] < path[pathTop - 1]) || find(uset, usetTop, nums[i])) + continue; + //将当前元素放入uset + uset[usetTop++] = nums[i]; + //将当前元素放入path + path[pathTop++] = nums[i]; + backTracking(nums, numsSize, i + 1); + //回溯 + pathTop--; + } +} + +int** findSubsequences(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ + //辅助数组初始化 + path = (int*)malloc(sizeof(int) * numsSize); + ans = (int**)malloc(sizeof(int*) * 33000); + length = (int*)malloc(sizeof(int*) * 33000); + pathTop = ansTop = 0; + + backTracking(nums, numsSize, 0); + + //设置数组中返回元素个数,以及每个一维数组的长度 + *returnSize = ansTop; + *returnColumnSizes = (int*)malloc(sizeof(int) * ansTop); + int i; + for(i = 0; i < ansTop; i++) { + (*returnColumnSizes)[i] = length[i]; + } + return ans; +} +``` ----------------------- diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 71e2dbb2..f3b33230 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -507,6 +507,8 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode { ## JavaScript +> 递归法: + ```javascript /** * Definition for a binary tree node. @@ -535,6 +537,53 @@ var mergeTrees = function (root1, root2) { return preOrder(root1, root2); }; ``` +> 迭代法: + +```javascript + +/** + * 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} root1 + * @param {TreeNode} root2 + * @return {TreeNode} + */ +var mergeTrees = function(root1, root2) { + if (root1 === null) return root2; + if (root2 === null) return root1; + + let queue = []; + queue.push(root1); + queue.push(root2); + while (queue.length) { + let node1 = queue.shift(); + let node2 = queue.shift();; + node1.val += node2.val; + if (node1.left !== null && node2.left !== null) { + queue.push(node1.left); + queue.push(node2.left); + } + if (node1.right !== null && node2.right !== null) { + queue.push(node1.right); + queue.push(node2.right); + } + if (node1.left === null && node2.left !== null) { + node1.left = node2.left; + } + if (node1.right === null && node2.right !== null) { + node1.right = node2.right; + } + } + return root1; +}; + +``` ----------------------- diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index dc9f2e74..92c23cab 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -288,15 +288,16 @@ func search(nums []int, target int) int { * @param {number} target * @return {number} */ +/** var search = function(nums, target) { - let left = 0, right = nums.length; - // 使用左闭右开区间 [left, right) - while (left < right) { + let left = 0, right = nums.length - 1; + // 使用左闭右闭区间 + while (left <= right) { let mid = left + Math.floor((right - left)/2); if (nums[mid] > target) { - right = mid; // 去左区间寻找 + right = mid - 1; // 去左面闭区间寻找 } else if (nums[mid] < target) { - left = mid + 1; // 去右区间寻找 + left = mid + 1; // 去右面闭区间寻找 } else { return mid; } diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 077b0e87..5dc9325b 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -880,6 +880,121 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { * obj.deleteAtIndex(index) */ ``` + +TypeScript: +```TypeScript +class ListNode { + public val: number; + public next: ListNode | null; + constructor(val?: number, next?: ListNode | null) { + this.val = val === undefined ? 0 : val; + this.next = next === undefined ? null : next; + } +} + +class MyLinkedList { + // 记录链表长度 + private size: number; + private head: ListNode | null; + private tail: ListNode | null; + constructor() { + this.size = 0; + this.head = null; + this.tail = null; + } + + // 获取链表中第 index个节点的值 + get(index: number): number { + // 索引无效的情况 + if (index < 0 || index >= this.size) { + return -1; + } + let curNode = this.getNode(index); + // 这里在前置条件下,理论上不会出现 null的情况 + return curNode.val; + } + + // 在链表的第一个元素之前添加一个值为 val的节点。插入后,新节点将成为链表的第一个节点。 + addAtHead(val: number): void { + let node: ListNode = new ListNode(val, this.head); + this.head = node; + if (!this.tail) { + this.tail = node; + } + this.size++; + } + + // 将值为 val 的节点追加到链表的最后一个元素。 + addAtTail(val: number): void { + let node: ListNode = new ListNode(val, null); + if (this.tail) { + this.tail.next = node; + } else { + // 还没有尾节点,说明一个节点都还没有 + this.head = node; + } + this.tail = node; + this.size++; + } + + // 在链表中的第 index个节点之前添加值为 val的节点。 + // 如果 index等于链表的长度,则该节点将附加到链表的末尾。如果 index大于链表长度,则不会插入节点。如果 index小于0,则在头部插入节点。 + addAtIndex(index: number, val: number): void { + if (index === this.size) { + this.addAtTail(val); + return; + } + if (index > this.size) { + return; + } + // <= 0 的情况都是在头部插入 + if (index <= 0) { + this.addAtHead(val); + return; + } + // 正常情况 + // 获取插入位置的前一个 node + let curNode = this.getNode(index - 1); + let node: ListNode = new ListNode(val, curNode.next); + curNode.next = node; + this.size++; + } + + // 如果索引 index有效,则删除链表中的第 index个节点。 + deleteAtIndex(index: number): void { + if (index < 0 || index >= this.size) { + return; + } + // 处理头节点 + if (index === 0) { + this.head = this.head!.next; + this.size--; + return; + } + // 索引有效 + let curNode: ListNode = this.getNode(index - 1); + curNode.next = curNode.next!.next; + // 处理尾节点 + if (index === this.size - 1) { + this.tail = curNode; + } + this.size--; + } + + // 获取指定 Node节点 + private getNode(index: number): ListNode { + // 这里不存在没办法获取到节点的情况,都已经在前置方法做过判断 + // 创建虚拟头节点 + let curNode: ListNode = new ListNode(0, this.head); + for (let i = 0; i <= index; i++) { + // 理论上不会出现 null + curNode = curNode.next!; + } + return curNode; + } +} +``` + Kotlin: ```kotlin class MyLinkedList { diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 7a9677ad..56feb20c 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -12,18 +12,24 @@ [力扣题目链接](https://leetcode-cn.com/problems/find-common-characters/) -给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。 +给你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案。 -你可以按任意顺序返回答案。 +示例 1: -【示例一】 -输入:["bella","label","roller"] +输入:words = ["bella","label","roller"] 输出:["e","l","l"] +示例 2: -【示例二】 -输入:["cool","lock","cook"] +输入:words = ["cool","lock","cook"] 输出:["c","o"] +提示: + +1 <= words.length <= 100 +1 <= words[i].length <= 100 +words[i] 由小写英文字母组成 + + # 思路 diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index 04f8de09..e9b03e24 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -109,6 +109,41 @@ class Solution: return dp[-1][-1] ``` + +Golang: + +```go + +func maxUncrossedLines(A []int, B []int) int { + m, n := len(A), len(B) + dp := make([][]int, m+1) + for i := range dp { + dp[i] = make([]int, n+1) + } + + for i := 1; i <= len(A); i++ { + for j := 1; j <= len(B); j++ { + if (A[i - 1] == B[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1 + } else { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + } + } + } + return dp[m][n] + +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + JavaScript: ```javascript diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index e097a2dd..6bb4ba3d 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -439,9 +439,84 @@ func traversal(root *TreeNode,result *[]string,path *[]int){ } ``` +JavaScript: +100.相同的树 +```javascript +/** + * 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} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function(p, q) { + if (p === null && q === null) { + return true; + } else if (p === null || q === null) { + return false; + } else if (p.val !== q.val) { + return false; + } else { + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + } +}; +``` +257.二叉树的不同路径 +> 回溯法: + +```javascript +/** + * 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 {string[]} + */ +var binaryTreePaths = function(root) { + const getPath = (root, path, result) => { + path.push(root.val); + if (root.left === null && root.right === null) { + let n = path.length; + let str = ''; + for (let i=0; i'; + } + str += path[n-1]; + result.push(str); + } + + if (root.left !== null) { + getPath(root.left, path, result); + path.pop(); // 回溯 + } + + if (root.right !== null) { + getPath(root.right, path, result); + path.pop(); + } + } + + if (root === null) return []; + let result = []; + let path = []; + getPath(root, path, result); + return result; +}; +``` -----------------------