Skip to content

Commit

Permalink
Merge branch 'master' into patch-4
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 authored May 15, 2021
2 parents 66cd701 + 78f2e76 commit 8c2e089
Show file tree
Hide file tree
Showing 19 changed files with 471 additions and 11 deletions.
27 changes: 27 additions & 0 deletions problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ class Solution {
return deque.isEmpty();
}
}
// 方法2
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
Map<Character, Character> map = new HashMap<Character, Character>() {
{
put('}', '{');
put(']', '[');
put(')', '(');
}
};
for (Character c : s.toCharArray()) { // 顺序读取字符
if (!stack.isEmpty() && map.containsKey(c)) { // 是右括号 && 栈不为空
if (stack.peek() == map.get(c)) { // 取其对应的左括号直接和栈顶比
stack.pop(); // 相同则抵消,出栈
} else {
return false; // 不同则直接返回
}
} else {
stack.push(c); // 左括号,直接入栈
}
}
return stack.isEmpty(); // 看左右是否抵消完
}
}
```

Python:
Expand Down
23 changes: 23 additions & 0 deletions problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public:


Java:

```Java
// 递归版本
class Solution {
public ListNode swapPairs(ListNode head) {
// base case 退出提交
Expand All @@ -104,6 +106,27 @@ class Solution {
}
```

```java
// 虚拟头结点
class Solution {
public ListNode swapPairs(ListNode head) {

ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode prev = dummyNode;

while (prev.next != null && prev.next.next != null) {
ListNode temp = head.next.next; // 缓存 next
prev.next = head.next; // 将 prev 的 next 改为 head 的 next
head.next.next = head; // 将 head.next(prev.next) 的next,指向 head
head.next = temp; // 将head 的 next 接上缓存的temp
prev = head; // 步进1位
head = head.next; // 步进1位
}
return dummyNode.next;
}
}
```

Python:

Expand Down
17 changes: 16 additions & 1 deletion problems/0045.跳跃游戏II.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,22 @@ class Solution {
```

Python:

```python
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1: return 0
ans = 0
curDistance = 0
nextDistance = 0
for i in range(len(nums)):
nextDistance = max(i + nums[i], nextDistance)
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
return ans
```

Go:

Expand Down
4 changes: 1 addition & 3 deletions problems/0046.全排列.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public:
Java:
```java
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
Expand All @@ -167,9 +168,6 @@ class Solution {
return;
}
for (int i = 0; i < nums.length; i++){
// if (path.contains(nums[i])){
// continue;
// }
if (used[i]){
continue;
}
Expand Down
38 changes: 38 additions & 0 deletions problems/0070.爬楼梯.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,45 @@ public:
Java:
```Java
class Solution {
public int climbStairs(int n) {
// 跟斐波那契数列一样
if(n <= 2) return n;
int a = 1, b = 2, sum = 0;
for(int i = 3; i <= n; i++){
sum = a + b;
a = b;
b = sum;
}
return b;
}
}
```

```java
// 常规方式
public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// 用变量记录代替数组
public int climbStairs(int n) {
int a = 0, b = 1, c = 0; // 默认需要1次
for (int i = 1; i <= n; i++) {
c = a + b; // f(i - 1) + f(n - 2)
a = b; // 记录上一轮的值
b = c; // 向后步进1个数
}
return c;
}
```

Python:

Expand Down
29 changes: 29 additions & 0 deletions problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,35 @@ class Solution {
return true;
}
}

// 简洁实现·递归解法
class Solution {
public boolean isValidBST(TreeNode root) {
return validBST(Long.MIN_VALUE, Long.MAX_VALUE, root);
}
boolean validBST(long lower, long upper, TreeNode root) {
if (root == null) return true;
if (root.val <= lower || root.val >= upper) return false;
return validBST(lower, root.val, root.left) && validBST(root.val, upper, root.right);
}
}
// 简洁实现·中序遍历
class Solution {
private long prev = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
if (!isValidBST(root.left)) {
return false;
}
if (root.val <= prev) { // 不满足二叉搜索树条件
return false;
}
prev = root.val;
return isValidBST(root.right);
}
}
```

Python:
Expand Down
13 changes: 13 additions & 0 deletions problems/0112.路径总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ class Solution {
}
}

// LC112 简洁方法
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {

if (root == null) return false; // 为空退出

// 叶子节点判断是否符合
if (root.left == null && root.right == null) return root.val == targetSum;

// 求两侧分支的路径和
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}
```

Python:
Expand Down
17 changes: 16 additions & 1 deletion problems/0121.买卖股票的最佳时机.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,22 @@ public:
Java:
```java
class Solution {
public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
```

Python:

Expand Down
27 changes: 27 additions & 0 deletions problems/0122.买卖股票的最佳时机II(动态规划).md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,34 @@ public:


Java:
```java
// 动态规划
class Solution
public int maxProfit(int[] prices) {
int n = prices.length;
int[][] dp = new int[n][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < n; ++i) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[n - 1][0];
}

public int maxProfit(int[] prices) {
int n = prices.length;
int dp0 = 0, dp1 = -prices[0];
for (int i = 1; i < n; ++i) {
int newDp0 = Math.max(dp0, dp1 + prices[i]);
int newDp1 = Math.max(dp1, dp0 - prices[i]);
dp0 = newDp0;
dp1 = newDp1;
}
return dp0;
}
}
```

Python:

Expand Down
27 changes: 26 additions & 1 deletion problems/0150.逆波兰表达式求值.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,32 @@ public class EvalRPN {
}
```

Go:
```Go
func evalRPN(tokens []string) int {
stack := []int{}
for _, token := range tokens {
val, err := strconv.Atoi(token)
if err == nil {
stack = append(stack, val)
} else {
num1, num2 := stack[len(stack)-2], stack[(len(stack))-1]
stack = stack[:len(stack)-2]
switch token {
case "+":
stack = append(stack, num1+num2)
case "-":
stack = append(stack, num1-num2)
case "*":
stack = append(stack, num1*num2)
case "/":
stack = append(stack, num1/num2)
}
}
}
return stack[0]
}
```



Expand Down
19 changes: 18 additions & 1 deletion problems/0198.打家劫舍.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,24 @@ public:
Java:
```Java
// 动态规划
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length == 1) return nums[0];
int[] dp = new int[nums.length + 1];
dp[0] = nums[0];
dp[1] = Math.max(dp[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[nums.length - 1];
}
}
```

Python:

Expand Down
21 changes: 21 additions & 0 deletions problems/0213.打家劫舍II.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,28 @@ public:
Java:
```Java
class Solution {
public int rob(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
if (len == 1)
return nums[0];
return Math.max(robAction(nums, 0, len - 1), robAction(nums, 1, len));
}
int robAction(int[] nums, int start, int end) {
int x = 0, y = 0, z = 0;
for (int i = start; i < end; i++) {
y = z;
z = Math.max(y, x + nums[i]);
x = y;
}
return z;
}
}
```

Python:

Expand Down
21 changes: 20 additions & 1 deletion problems/0300.最长上升子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,26 @@ public:
Java:
```Java
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int res = 0;
for (int i = 0; i < dp.length; i++) {
res = Math.max(res, dp[i]);
}
return res;
}
}
```

Python:

Expand Down
Loading

0 comments on commit 8c2e089

Please sign in to comment.