Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Sep 29, 2021
2 parents 5de0f00 + 93a40f9 commit 4c87a26
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 15 deletions.
27 changes: 27 additions & 0 deletions problems/0005.最长回文子串.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
136 changes: 132 additions & 4 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
```

Expand Down Expand Up @@ -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<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
Expand Down Expand Up @@ -1426,7 +1461,39 @@ class Solution:
first = dummyHead.next # 此处为换行操作,更新到下一行
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
Expand Down Expand Up @@ -1608,6 +1675,36 @@ func maxDepth(root *TreeNode) int {


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.二叉树的最小深度

Expand Down Expand Up @@ -1746,9 +1843,40 @@ func minDepth(root *TreeNode) int {
}
```



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;
};
```



Expand Down
65 changes: 65 additions & 0 deletions problems/0491.递增子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
-----------------------
Expand Down
49 changes: 49 additions & 0 deletions problems/0617.合并二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {

## JavaScript

> 递归法:

```javascript
/**
* Definition for a binary tree node.
Expand Down Expand Up @@ -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;
};

```


-----------------------
Expand Down
11 changes: 6 additions & 5 deletions problems/0704.二分查找.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 4c87a26

Please sign in to comment.