mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 13:00:22 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
|
||||
|
||||
👉 推荐 [在线阅读](http://programmercarl.com/) (Github在国内访问经常不稳定)
|
||||
👉 推荐 [Gitee同步](https://gitee.com/programmercarl/leetcode-master)
|
||||
|
||||
|
@ -1287,23 +1287,23 @@ java代码:
|
||||
```java
|
||||
class Solution {
|
||||
public List<Integer> largestValues(TreeNode root) {
|
||||
List<Integer> retVal = new ArrayList<Integer>();
|
||||
Queue<TreeNode> tmpQueue = new LinkedList<TreeNode>();
|
||||
if (root != null) tmpQueue.add(root);
|
||||
|
||||
while (tmpQueue.size() != 0){
|
||||
int size = tmpQueue.size();
|
||||
List<Integer> lvlVals = new ArrayList<Integer>();
|
||||
for (int index = 0; index < size; index++){
|
||||
TreeNode node = tmpQueue.poll();
|
||||
lvlVals.add(node.val);
|
||||
if (node.left != null) tmpQueue.add(node.left);
|
||||
if (node.right != null) tmpQueue.add(node.right);
|
||||
}
|
||||
retVal.add(Collections.max(lvlVals));
|
||||
}
|
||||
|
||||
return retVal;
|
||||
if(root == null){
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<Integer> result = new ArrayList();
|
||||
Queue<TreeNode> queue = new LinkedList();
|
||||
queue.offer(root);
|
||||
while(!queue.isEmpty()){
|
||||
int max = Integer.MIN_VALUE;
|
||||
for(int i = queue.size(); i > 0; i--){
|
||||
TreeNode node = queue.poll();
|
||||
max = Math.max(max, node.val);
|
||||
if(node.left != null) queue.offer(node.left);
|
||||
if(node.right != null) queue.offer(node.right);
|
||||
}
|
||||
result.add(max);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -205,6 +205,29 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```java
|
||||
//另一种解题思路
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
int[][] dp = new int[prices.length + 1][2];
|
||||
dp[1][0] = -prices[0];
|
||||
|
||||
for (int i = 2; i <= prices.length; i++) {
|
||||
/*
|
||||
dp[i][0] 第i天未持有股票收益;
|
||||
dp[i][1] 第i天持有股票收益;
|
||||
情况一:第i天是冷静期,不能以dp[i-1][1]购买股票,所以以dp[i - 2][1]买股票,没问题
|
||||
情况二:第i天不是冷静期,理论上应该以dp[i-1][1]购买股票,但是第i天不是冷静期说明,第i-1天没有卖出股票,
|
||||
则dp[i-1][1]=dp[i-2][1],所以可以用dp[i-2][1]买股票,没问题
|
||||
*/
|
||||
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 2][1] - prices[i - 1]);
|
||||
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i - 1]);
|
||||
}
|
||||
|
||||
return dp[prices.length][1];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
@ -251,14 +251,14 @@ Python:
|
||||
```python
|
||||
class Solution:
|
||||
def canPartition(self, nums: List[int]) -> bool:
|
||||
taraget = sum(nums)
|
||||
if taraget % 2 == 1: return False
|
||||
taraget //= 2
|
||||
target = sum(nums)
|
||||
if target % 2 == 1: return False
|
||||
target //= 2
|
||||
dp = [0] * 10001
|
||||
for i in range(len(nums)):
|
||||
for j in range(taraget, nums[i] - 1, -1):
|
||||
for j in range(target, nums[i] - 1, -1):
|
||||
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
|
||||
return taraget == dp[taraget]
|
||||
return target == dp[target]
|
||||
```
|
||||
Go:
|
||||
```go
|
||||
|
Reference in New Issue
Block a user