Skip to content

Commit

Permalink
Merge branch 'master' into tree15
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 authored Feb 26, 2022
2 parents 49a1a42 + 83a9d8f commit d9a9418
Show file tree
Hide file tree
Showing 57 changed files with 1,670 additions and 332 deletions.
Binary file added .DS_Store
Binary file not shown.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

> 1. **介绍**:本项目是一套完整的刷题计划,旨在帮助大家少走弯路,循序渐进学算法,[关注作者](#关于作者)
> 2. **PDF版本**[「代码随想录」算法精讲 PDF 版本](https://programmercarl.com/other/algo_pdf.html)
> 3. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
> 4. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)
> 5. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
> 6. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
> 3. **最强八股文:**[代码随想录知识星球精华PDF](https://www.programmercarl.com/other/kstar_baguwen.html)
> 4. **刷题顺序** : README已经将刷题顺序排好了,按照顺序一道一道刷就可以。
> 5. **学习社区** : 一起学习打卡/面试技巧/如何选择offer/大厂内推/职场规则/简历修改/技术分享/程序人生。欢迎加入[「代码随想录」知识星球](https://programmercarl.com/other/kstar.html)
> 6. **提交代码**:本项目统一使用C++语言进行讲解,但已经有Java、Python、Go、JavaScript等等多语言版本,感谢[这里的每一位贡献者](https://github.com/youngyangyang04/leetcode-master/graphs/contributors),如果你也想贡献代码点亮你的头像,[点击这里](https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A)了解提交代码的方式。
> 7. **转载须知** :以下所有文章皆为我([程序员Carl](https://github.com/youngyangyang04))的原创。引用本项目文章请注明出处,发现恶意抄袭或搬运,会动用法律武器维护自己的权益。让我们一起维护一个良好的技术创作环境!
<p align="center">
<a href="programmercarl.com" target="_blank">
Expand Down
52 changes: 52 additions & 0 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,60 @@ var isValid = function(s) {
};
```

TypeScript:

版本一:普通版

```typescript
function isValid(s: string): boolean {
let helperStack: string[] = [];
for (let i = 0, length = s.length; i < length; i++) {
let x: string = s[i];
switch (x) {
case '(':
helperStack.push(')');
break;
case '[':
helperStack.push(']');
break;
case '{':
helperStack.push('}');
break;
default:
if (helperStack.pop() !== x) return false;
break;
}
}
return helperStack.length === 0;
};
```

版本二:优化版

```typescript
function isValid(s: string): boolean {
type BracketMap = {
[index: string]: string;
}
let helperStack: string[] = [];
let bracketMap: BracketMap = {
'(': ')',
'[': ']',
'{': '}'
}
for (let i of s) {
if (bracketMap.hasOwnProperty(i)) {
helperStack.push(bracketMap[i]);
} else if (i !== helperStack.pop()) {
return false;
}
}
return helperStack.length === 0;
};
```

Swift

```swift
func isValid(_ s: String) -> Bool {
var stack = [String.Element]()
Expand Down
2 changes: 1 addition & 1 deletion problems/0031.下一个排列.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public:
for (int j = nums.size() - 1; j > i; j--) {
if (nums[j] > nums[i]) {
swap(nums[j], nums[i]);
sort(nums.begin() + i + 1, nums.end());
reverse(nums.begin() + i + 1, nums.end());
return;
}
}
Expand Down
24 changes: 24 additions & 0 deletions problems/0045.跳跃游戏II.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public:

### Java
```Java
// 版本一
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length == 0 || nums.length == 1) {
Expand Down Expand Up @@ -172,7 +173,30 @@ class Solution {
}
```

```java
// 版本二
class Solution {
public int jump(int[] nums) {
int result = 0;
// 当前覆盖的最远距离下标
int end = 0;
// 下一步覆盖的最远距离下标
int temp = 0;
for (int i = 0; i <= end && end < nums.length - 1; ++i) {
temp = Math.max(temp, i + nums[i]);
// 可达位置的改变次数就是跳跃次数
if (i == end) {
end = temp;
result++;
}
}
return result;
}
}
```

### Python

```python
class Solution:
def jump(self, nums: List[int]) -> int:
Expand Down
29 changes: 14 additions & 15 deletions problems/0063.不同路径II.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>

## 63. 不同路径 II
# 63. 不同路径 II

[力扣题目链接](https://leetcode-cn.com/problems/unique-paths-ii/)

Expand All @@ -22,23 +22,22 @@

![](https://img-blog.csdnimg.cn/20210111204939971.png)

输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
* 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
* 输出:2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右
* 3x3 网格的正中间有一个障碍物。
* 从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右

示例 2:

![](https://img-blog.csdnimg.cn/20210111205857918.png)

输入:obstacleGrid = [[0,1],[0,0]]
输出:1
* 输入:obstacleGrid = [[0,1],[0,0]]
* 输出:1

提示:

* m == obstacleGrid.length
* n == obstacleGrid[i].length
* 1 <= m, n <= 100
Expand Down Expand Up @@ -171,7 +170,7 @@ public:
## 其他语言版本
Java
### Java
```java
class Solution {
Expand Down Expand Up @@ -199,7 +198,7 @@ class Solution {
```


Python
### Python

```python
class Solution:
Expand Down Expand Up @@ -262,7 +261,7 @@ class Solution:
```


Go:
### Go

```go
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
Expand Down Expand Up @@ -295,8 +294,8 @@ func uniquePathsWithObstacles(obstacleGrid [][]int) int {

```

Javascript
``` Javascript
### Javascript
```Javascript
var uniquePathsWithObstacles = function(obstacleGrid) {
const m = obstacleGrid.length
const n = obstacleGrid[0].length
Expand Down
10 changes: 5 additions & 5 deletions problems/0096.不同的二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>

## 96.不同的二叉搜索树
# 96.不同的二叉搜索树

[力扣题目链接](https://leetcode-cn.com/problems/unique-binary-search-trees/)

Expand Down Expand Up @@ -163,7 +163,7 @@ public:
## 其他语言版本
Java
### Java
```Java
class Solution {
public int numTrees(int n) {
Expand All @@ -184,7 +184,7 @@ class Solution {
}
```

Python
### Python
```python
class Solution:
def numTrees(self, n: int) -> int:
Expand All @@ -196,7 +196,7 @@ class Solution:
return dp[-1]
```

Go:
### Go
```Go
func numTrees(n int)int{
dp:=make([]int,n+1)
Expand All @@ -210,7 +210,7 @@ func numTrees(n int)int{
}
```

Javascript
### Javascript
```Javascript
const numTrees =(n) => {
let dp = new Array(n+1).fill(0);
Expand Down
69 changes: 69 additions & 0 deletions problems/0101.对称二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,75 @@ var isSymmetric = function(root) {
};
```

## TypeScript:

> 递归法

```typescript
function isSymmetric(root: TreeNode | null): boolean {
function recur(node1: TreeNode | null, node2: TreeNode | null): boolean {
if (node1 === null && node2 === null) return true;
if (node1 === null || node2 === null) return false;
if (node1.val !== node2.val) return false
let isSym1: boolean = recur(node1.left, node2.right);
let isSym2: boolean = recur(node1.right, node2.left);
return isSym1 && isSym2
}
if (root === null) return true;
return recur(root.left, root.right);
};
```
> 迭代法
```typescript
// 迭代法(队列)
function isSymmetric(root: TreeNode | null): boolean {
let helperQueue: (TreeNode | null)[] = [];
let tempNode1: TreeNode | null,
tempNode2: TreeNode | null;
if (root !== null) {
helperQueue.push(root.left);
helperQueue.push(root.right);
}
while (helperQueue.length > 0) {
tempNode1 = helperQueue.shift()!;
tempNode2 = helperQueue.shift()!;
if (tempNode1 === null && tempNode2 === null) continue;
if (tempNode1 === null || tempNode2 === null) return false;
if (tempNode1.val !== tempNode2.val) return false;
helperQueue.push(tempNode1.left);
helperQueue.push(tempNode2.right);
helperQueue.push(tempNode1.right);
helperQueue.push(tempNode2.left);
}
return true;
}
// 迭代法(栈)
function isSymmetric(root: TreeNode | null): boolean {
let helperStack: (TreeNode | null)[] = [];
let tempNode1: TreeNode | null,
tempNode2: TreeNode | null;
if (root !== null) {
helperStack.push(root.left);
helperStack.push(root.right);
}
while (helperStack.length > 0) {
tempNode1 = helperStack.pop()!;
tempNode2 = helperStack.pop()!;
if (tempNode1 === null && tempNode2 === null) continue;
if (tempNode1 === null || tempNode2 === null) return false;
if (tempNode1.val !== tempNode2.val) return false;
helperStack.push(tempNode1.left);
helperStack.push(tempNode2.right);
helperStack.push(tempNode1.right);
helperStack.push(tempNode2.left);
}
return true;
};
```

## Swift:

> 递归
Expand Down
Loading

0 comments on commit d9a9418

Please sign in to comment.