Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
youngyangyang04
2021-09-29 11:28:56 +08:00
9 changed files with 516 additions and 15 deletions

View File

@ -288,7 +288,34 @@ class Solution:
return s[left:right + 1] 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
```go ```go

View File

@ -421,9 +421,10 @@ var levelOrderBottom = function(root) {
node.left&&queue.push(node.left); node.left&&queue.push(node.left);
node.right&&queue.push(node.right); 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 # 从本层扩展到下一层 first = first.left # 从本层扩展到下一层
return root 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<n; i++) {
let node = queue.shift();
if (i < n-1) {
node.next = queue[0];
}
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
}
return root;
};
```
go: go:
```GO ```GO
@ -1426,7 +1461,39 @@ class Solution:
first = dummyHead.next # 此处为换行操作,更新到下一行 first = dummyHead.next # 此处为换行操作,更新到下一行
return root 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 null;
}
let queue = [root];
while (queue.length > 0) {
let n = queue.length;
for (let i=0; i<n; i++) {
let node = queue.shift();
if (i < n-1) node.next = queue[0];
if (node.left != null) queue.push(node.left);
if (node.right != null) queue.push(node.right);
}
}
return root;
};
```
go: go:
```GO ```GO
@ -1608,6 +1675,36 @@ func maxDepth(root *TreeNode) int {
JavaScript JavaScript
```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 {number}
*/
var maxDepth = function(root) {
// 最大的深度就是二叉树的层数
if (root === null) return 0;
let queue = [root];
let height = 0;
while (queue.length) {
let n = queue.length;
height++;
for (let i=0; i<n; i++) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
}
return height;
};
```
# 111.二叉树的最小深度 # 111.二叉树的最小深度
@ -1746,9 +1843,40 @@ func minDepth(root *TreeNode) int {
} }
``` ```
JavaScript JavaScript
```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 {number}
*/
var minDepth = function(root) {
if (root === null) return 0;
let queue = [root];
let deepth = 0;
while (queue.length) {
let n = queue.length;
deepth++;
for (let i=0; i<n; i++) {
let node = queue.shift();
// 如果左右节点都是null则该节点深度最小
if (node.left === null && node.right === null) {
return deepth;
}
node.left && queue.push(node.left);;
node.right && queue.push (node.right);
}
}
return deepth;
};
```

View File

@ -313,7 +313,72 @@ var findSubsequences = function(nums) {
``` ```
C:
```c
int* path;
int pathTop;
int** ans;
int ansTop;
int* length;
//将当前path中的内容复制到ans中
void copy() {
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
memcpy(tempPath, path, pathTop * sizeof(int));
length[ansTop] = pathTop;
ans[ansTop++] = tempPath;
}
//查找uset中是否存在值为key的元素
int find(int* uset, int usetSize, int key) {
int i;
for(i = 0; i < usetSize; i++) {
if(uset[i] == key)
return 1;
}
return 0;
}
void backTracking(int* nums, int numsSize, int startIndex) {
//当path中元素大于1个时将path拷贝到ans中
if(pathTop > 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;
}
```
----------------------- -----------------------

View File

@ -507,6 +507,8 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
## JavaScript ## JavaScript
> 递归法:
```javascript ```javascript
/** /**
* Definition for a binary tree node. * Definition for a binary tree node.
@ -535,6 +537,53 @@ var mergeTrees = function (root1, root2) {
return preOrder(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;
};
```
----------------------- -----------------------

View File

@ -288,15 +288,16 @@ func search(nums []int, target int) int {
* @param {number} target * @param {number} target
* @return {number} * @return {number}
*/ */
/**
var search = function(nums, target) { var search = function(nums, target) {
let left = 0, right = nums.length; let left = 0, right = nums.length - 1;
// 使用左闭右区间 [left, right) // 使用左闭右区间
while (left < right) { while (left <= right) {
let mid = left + Math.floor((right - left)/2); let mid = left + Math.floor((right - left)/2);
if (nums[mid] > target) { if (nums[mid] > target) {
right = mid; // 去左区间寻找 right = mid - 1; // 去左面闭区间寻找
} else if (nums[mid] < target) { } else if (nums[mid] < target) {
left = mid + 1; // 去右区间寻找 left = mid + 1; // 去右面闭区间寻找
} else { } else {
return mid; return mid;
} }

View File

@ -880,6 +880,121 @@ MyLinkedList.prototype.deleteAtIndex = function(index) {
* obj.deleteAtIndex(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:
```kotlin ```kotlin
class MyLinkedList { class MyLinkedList {

View File

@ -12,18 +12,24 @@
[力扣题目链接](https://leetcode-cn.com/problems/find-common-characters/) [力扣题目链接](https://leetcode-cn.com/problems/find-common-characters/)
定仅有小写字母组成的字符串数组 A返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次 你一个字符串数组 words ,请你找出所有在 words 的每个字符串中都出现的共用字符( 包括重复字符),并以数组形式返回。你可以按 任意顺序 返回答案
你可以按任意顺序返回答案。 示例 1
【示例一】 输入words = ["bella","label","roller"]
输入:["bella","label","roller"]
输出:["e","l","l"] 输出:["e","l","l"]
示例 2
【示例二】 输入words = ["cool","lock","cook"]
输入:["cool","lock","cook"]
输出:["c","o"] 输出:["c","o"]
提示:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] 由小写英文字母组成
# 思路 # 思路

View File

@ -109,6 +109,41 @@ class Solution:
return dp[-1][-1] 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
```javascript ```javascript

View File

@ -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<n-1; i++) {
str += path[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;
};
```
----------------------- -----------------------