mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
@ -89,9 +89,9 @@ TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
|
||||
|
||||
**难点大家应该发现了,就是如何切割,以及边界值找不好很容易乱套。**
|
||||
|
||||
此时应该注意确定切割的标准,是左闭右开,还有左开又闭,还是左闭又闭,这个就是不变量,要在递归中保持这个不变量。
|
||||
此时应该注意确定切割的标准,是左闭右开,还有左开右闭,还是左闭右闭,这个就是不变量,要在递归中保持这个不变量。
|
||||
|
||||
**在切割的过程中会产生四个区间,把握不好不变量的话,一会左闭右开,一会左闭又闭,必然乱套!**
|
||||
**在切割的过程中会产生四个区间,把握不好不变量的话,一会左闭右开,一会左闭右闭,必然乱套!**
|
||||
|
||||
我在[数组:每次遇到二分法,都是一看就会,一写就废](https://programmercarl.com/0035.搜索插入位置.html)和[数组:这个循环可以转懵很多人!](https://programmercarl.com/0059.螺旋矩阵II.html)中都强调过循环不变量的重要性,在二分查找以及螺旋矩阵的求解中,坚持循环不变量非常重要,本题也是。
|
||||
|
||||
|
@ -227,7 +227,39 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
//法二:使用map
|
||||
class Solution {
|
||||
//结果集合
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
//路径集合
|
||||
LinkedList<Integer> path = new LinkedList<>();
|
||||
public List<List<Integer>> findSubsequences(int[] nums) {
|
||||
getSubsequences(nums,0);
|
||||
return res;
|
||||
}
|
||||
private void getSubsequences( int[] nums, int start ) {
|
||||
if(path.size()>1 ){
|
||||
res.add( new ArrayList<>(path) );
|
||||
// 注意这里不要加return,要取树上的节点
|
||||
}
|
||||
HashMap<Integer,Integer> map = new HashMap<>();
|
||||
for(int i=start ;i < nums.length ;i++){
|
||||
if(!path.isEmpty() && nums[i]< path.getLast()){
|
||||
continue;
|
||||
}
|
||||
// 使用过了当前数字
|
||||
if ( map.getOrDefault( nums[i],0 ) >=1 ){
|
||||
continue;
|
||||
}
|
||||
map.put(nums[i],map.getOrDefault( nums[i],0 )+1);
|
||||
path.add( nums[i] );
|
||||
getSubsequences( nums,i+1 );
|
||||
path.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
|
@ -128,24 +128,24 @@ public:
|
||||
```java
|
||||
class Solution {
|
||||
public boolean lemonadeChange(int[] bills) {
|
||||
int cash_5 = 0;
|
||||
int cash_10 = 0;
|
||||
int five = 0;
|
||||
int ten = 0;
|
||||
|
||||
for (int i = 0; i < bills.length; i++) {
|
||||
if (bills[i] == 5) {
|
||||
cash_5++;
|
||||
five++;
|
||||
} else if (bills[i] == 10) {
|
||||
cash_5--;
|
||||
cash_10++;
|
||||
five--;
|
||||
ten++;
|
||||
} else if (bills[i] == 20) {
|
||||
if (cash_10 > 0) {
|
||||
cash_10--;
|
||||
cash_5--;
|
||||
if (ten > 0) {
|
||||
ten--;
|
||||
five--;
|
||||
} else {
|
||||
cash_5 -= 3;
|
||||
five -= 3;
|
||||
}
|
||||
}
|
||||
if (cash_5 < 0 || cash_10 < 0) return false;
|
||||
if (five < 0 || ten < 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user