Skip to content

Commit

Permalink
Merge branch 'master' into patch-18
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed May 14, 2021
2 parents 001a4dc + bf0db08 commit 62d3c60
Show file tree
Hide file tree
Showing 58 changed files with 1,888 additions and 95 deletions.
17 changes: 17 additions & 0 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ func isValid(s string) bool {
}
```

Ruby:
```ruby
def is_valid(strs)
symbol_map = {')' => '(', '}' => '{', ']' => '['}
stack = []
strs.size.times {|i|
c = strs[i]
if symbol_map.has_key?(c)
top_e = stack.shift
return false if symbol_map[c] != top_e
else
stack.unshift(c)
end
}
stack.empty?
end
```


-----------------------
Expand Down
20 changes: 19 additions & 1 deletion problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ public:
Java:
```java
class Solution {
public int removeElement(int[] nums, int val) {
// 快慢指针
int fastIndex = 0;
int slowIndex;
for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
if (nums[fastIndex] != val) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
}
```

Python:

Expand Down Expand Up @@ -172,4 +190,4 @@ var removeElement = (nums, val) => {
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
48 changes: 48 additions & 0 deletions problems/0028.实现strStr.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,54 @@ public:

Java:

```Java
class Solution {
/**
* 基于窗口滑动的算法
* <p>
* 时间复杂度:O(m*n)
* 空间复杂度:O(1)
* 注:n为haystack的长度,m为needle的长度
*/
public int strStr(String haystack, String needle) {
int m = needle.length();
// 当 needle 是空字符串时我们应当返回 0
if (m == 0) {
return 0;
}
int n = haystack.length();
if (n < m) {
return -1;
}
int i = 0;
int j = 0;
while (i < n - m + 1) {
// 找到首字母相等
while (i < n && haystack.charAt(i) != needle.charAt(j)) {
i++;
}
if (i == n) {// 没有首字母相等的
return -1;
}
// 遍历后续字符,判断是否相等
i++;
j++;
while (i < n && j < m && haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
}
if (j == m) {// 找到
return i - j;
} else {// 未找到
i -= j - 1;
j = 0;
}
}
return -1;
}
}
```

```java
// 方法一
class Solution {
Expand Down
14 changes: 13 additions & 1 deletion problems/0055.跳跃游戏.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,19 @@ class Solution {
```

Python:

```python
class Solution:
def canJump(self, nums: List[int]) -> bool:
cover = 0
if len(nums) == 1: return True
i = 0
# python不支持动态修改for循环中变量,使用while循环代替
while i <= cover:
cover = max(i + nums[i], cover)
if cover >= len(nums) - 1: return True
i += 1
return False
```

Go:

Expand Down
30 changes: 29 additions & 1 deletion problems/0056.合并区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,35 @@ public:


Java:
```java
class Solution {
public int[][] merge(int[][] intervals) {
List<int[]> res = new LinkedList<>();
Arrays.sort(intervals, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] != o2[0]) {
return Integer.compare(o1[0],o2[0]);
} else {
return Integer.compare(o1[1],o2[1]);
}
}
});

int start = intervals[0][0];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] > intervals[i - 1][1]) {
res.add(new int[]{start, intervals[i - 1][1]});
start = intervals[i][0];
} else {
intervals[i][1] = Math.max(intervals[i][1], intervals[i - 1][1]);
}
}
res.add(new int[]{start, intervals[intervals.length - 1][1]});
return res.toArray(new int[res.size()][]);
}
}
```

Python:

Expand All @@ -151,4 +179,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
18 changes: 17 additions & 1 deletion problems/0070.爬楼梯完全背包版本.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,23 @@ public:


Java:
```java
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
int[] weight = {1,2};
dp[0] = 1;

for (int i = 0; i <= n; i++) {
for (int j = 0; j < weight.length; j++) {
if (i >= weight[j]) dp[i] += dp[i - weight[j]];
}
}

return dp[n];
}
}
```

Python:

Expand All @@ -141,4 +157,4 @@ Go:
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
15 changes: 14 additions & 1 deletion problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ public:


Java:
```java
class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST_2(root, Long.MIN_VALUE, Long.MAX_VALUE);
}

public boolean isValidBST_2 (TreeNode root, long lo, long hi) {
if (root == null) return true;
if (root.val >= hi || root.val <= lo) return false;
return isValidBST_2(root.left,lo,root.val) && isValidBST_2(root.right,root.val,hi);
}
}
```


Python:
Expand Down Expand Up @@ -286,4 +299,4 @@ func isBST(root *TreeNode, min, max int) bool {
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
89 changes: 88 additions & 1 deletion problems/0101.对称二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,94 @@ public:
## 其他语言版本

Java:
```java
public class N0101 {
/**
* 解法1:DFS,递归。
*/
public boolean isSymmetric2(TreeNode root) {
if (root == null) {
return false;
}

return compare(root.left, root.right);
}

private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left != null && right == null) {
return false;
}
if (left == null && right != null) {
return false;
}

if (left.val == right.val) {
return compare(left.left, right.right) && compare(left.right, right.left);
}

return false;
}

/**
* 解法2:DFS,迭代
*/
public boolean isSymmetric3(TreeNode root) {
if (root == null) {
return false;
}

if (!equal(root.left, root.right)) {
return false;
}

Deque<TreeNode> st = new LinkedList<>();

st.push(root.right);
st.push(root.left);

TreeNode curR = root.right;
TreeNode curL = root.left;

while (!st.isEmpty()) {
curL = st.pop();
curR = st.pop();

// 前序,处理
if (!equal(curL, curR)) {
return false;
}

if (curR != null && curL != null) {
st.push(curL.right);
st.push(curR.left);
st.push(curR.right);
st.push(curL.left);
}
}

return true;
}

private boolean equal(TreeNode l, TreeNode r) {
if (l == null && r == null) {
return true;
}
if (l != null && r == null) {
return false;
}
if (l == null && r != null) {
return false;
}
if (l.val == r.val) {
return true;
}
return false;
}
}
```

Python:

Expand Down Expand Up @@ -283,4 +370,4 @@ const check = (leftPtr, rightPtr) => {
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
Loading

0 comments on commit 62d3c60

Please sign in to comment.