Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
suichenglixin committed May 13, 2021
1 parent ce60363 commit d2dc300
Show file tree
Hide file tree
Showing 45 changed files with 984 additions and 60 deletions.
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 All @@ -268,4 +281,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>
14 changes: 13 additions & 1 deletion problems/0104.二叉树的最大深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ public:


Java:
```java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftdeep = maxDepth(root.left);
int rightdeep = maxDepth(root.right);
return 1+Math.max(leftdeep,rightdeep);
}
}
```


Python:
Expand All @@ -245,4 +257,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>
43 changes: 42 additions & 1 deletion problems/0106.从中序与后序遍历序列构造二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,47 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
Java:
```java
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 || postorder.length == 0) return null;
return buildTree(inorder, postorder,0,inorder.length,0,postorder.length);
}
private TreeNode buildTree(int[] inorder, int[] postorder, int infrom, int into,
int postfrom, int postto) {
if (postfrom == postto) return null;
int rootValue = postorder[postto - 1];
TreeNode root = new TreeNode(rootValue);
if (postfrom + 1 == postto) return root;
int splitNum = postto - 1;
for (int i = infrom; i < into; i++) {
if (inorder[i] == rootValue) {
splitNum = i;
break;
}
}
int inLeftBegin = infrom;
int inLeftEnd = splitNum;
int inRightBegin = splitNum + 1;
int inRightEnd = into;
int postLeftBegin = postfrom;
int postLeftEnd = postLeftBegin + (splitNum - inLeftBegin);
int postRightBegin = postLeftBegin + (splitNum - inLeftBegin);
int postRightEnd = postto - 1;
root.left = buildTree(inorder,postorder,inLeftBegin,inLeftEnd,postLeftBegin,postLeftEnd);
root.right = buildTree(inorder,postorder,inRightBegin,inRightEnd,postRightBegin,postRightEnd);
return root;
}
}
```


Python:
Expand All @@ -596,4 +637,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>
22 changes: 21 additions & 1 deletion problems/0108.将有序数组转换为二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ public:


Java:
```java
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return sortArr(nums,0,nums.length);
}

private TreeNode sortArr(int[] nums, int lo, int hi) {
if (lo == hi) return null;
int rootIdx = (lo + hi)/2;

int rootValue = nums[rootIdx];
TreeNode root = new TreeNode(rootValue);

root.left = sortArr(nums,lo,rootIdx);
root.right = sortArr(nums,rootIdx+1,hi);

return root;
}
}
```


Python:
Expand All @@ -223,4 +243,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>
25 changes: 24 additions & 1 deletion problems/0110.平衡二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,29 @@ public:


Java:
```java
class Solution {
boolean flag = true;
public boolean isBalanced(TreeNode root) {
high(root);
return flag;
}

private int high (TreeNode root) {
if (root == null) return 0;
if (flag) {
int left = high(root.left);
int right = high(root.right);

if (Math.abs(left - right) > 1) flag = false;

return Math.max(left,right) + 1;
} else {
return -1;
}
}
}
```


Python:
Expand All @@ -369,4 +392,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>
14 changes: 14 additions & 0 deletions problems/0111.二叉树的最小深度.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ public:


Java:
```java
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
int leftDeep = minDepth(root.left);
int rightDeep = minDepth(root.right);

if (root.left == null && root.right != null) return 1+rightDeep;
if (root.left != null && root.right == null) return 1+leftDeep;

return Math.min(leftDeep,rightDeep)+1;
}
}
```

Python:

Expand Down
20 changes: 19 additions & 1 deletion problems/0112.路径总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@ public:


Java:
```java
class Solution {
private boolean flag = false;
public boolean hasPathSum(TreeNode root, int targetSum) {
has_2(root,targetSum);
return flag;
}

public void has_2 (TreeNode root, int count) {
if (root == null) return;
if (root.left == null && root.right == null && count == root.val) {
flag = true;
}
if (root.left != null) has_2(root.left,count - root.val);
if (root.right != null) has_2(root.right,count - root.val);
}
}
```


Python:
Expand Down Expand Up @@ -387,4 +405,4 @@ let pathSum = function (root, targetSum) {
* 作者微信:[程序员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>
20 changes: 18 additions & 2 deletions problems/0122.买卖股票的最佳时机II.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ public:


Java:

```java
class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
int profit = 0;
int buy = prices[0];
for (int i = 1; i < prices.length; i++) {
profit = prices[i] - buy;
if (profit > 0) {
sum += profit;
}
buy = prices[i];
}
return sum;
}
}
```

Python:

Expand All @@ -149,4 +165,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>
23 changes: 22 additions & 1 deletion problems/0134.加油站.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,28 @@ public:
Java:
```java
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int sum = 0;
int min = 0;
for (int i = 0; i < gas.length; i++) {
sum += (gas[i] - cost[i]);
min = Math.min(sum, min);
}
if (sum < 0) return -1;
if (min >= 0) return 0;
for (int i = gas.length - 1; i > 0; i--) {
min += (gas[i] - cost[i]);
if (min >= 0) return i;
}
return -1;
}
}
```

Python:

Expand All @@ -213,4 +234,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>
Loading

0 comments on commit d2dc300

Please sign in to comment.