Skip to content

Commit eb9a286

Browse files
committed
add leetcode: 1117,39,42
1 parent 43937ae commit eb9a286

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetCode.multiThread;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-31-21:37
6+
* @Function Description :1117.H2O 生成
7+
*/
8+
public class _1117H2OGeneration {
9+
class H2O {
10+
private volatile int state = 0 ;
11+
private Object lock = new Object();
12+
13+
public H2O() {
14+
15+
}
16+
17+
public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
18+
synchronized(lock){
19+
// state等于2,说明对于H来说,已经有了2个;阻塞当前H线程
20+
while( state == 2 ){
21+
lock.wait();
22+
}
23+
// H线程不满2个的时候可以执行,state对应相加
24+
state++;
25+
releaseHydrogen.run();
26+
lock.notifyAll();
27+
}
28+
29+
30+
}
31+
32+
public void oxygen(Runnable releaseOxygen) throws InterruptedException {
33+
synchronized(lock){
34+
// 在O线程里面,state不等于2,说明H还不满,所以需要阻塞O线程
35+
while( state != 2 ){
36+
lock.wait();
37+
}
38+
// state等于2满足H,这个时候可以执行O线程,同时重置state。组合一个H2O
39+
state = 0;
40+
releaseOxygen.run();
41+
lock.notifyAll();
42+
}
43+
44+
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetCode.subject.number1_50;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-31-22:10
9+
* @Function Description :39.组合总和
10+
*/
11+
public class _39SumOfCombination {
12+
class Solution {
13+
/**因为要考虑所有的方案,所以不能优化。
14+
爆搜:因为每个数可以重复选择。所以搜索的时候枚举当前数,考虑选0次的情况,
15+
1次的情况,2次的情况等等。。。
16+
17+
*/
18+
// res答案 path搜索一次的方案
19+
private List<List<Integer>> res = new ArrayList<>();
20+
private List<Integer> path = new ArrayList<>();
21+
22+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
23+
//从第一个数开始枚举
24+
dfs( candidates , 0 , target );
25+
return res;
26+
}
27+
28+
// 每选用一个数就把target对应相减,减到0,可以组合
29+
public void dfs( int[] cs , int u , int target ){
30+
// 枚举到可以组合target.把当前遍历的路径添加到res中
31+
if( target == 0 ){
32+
// ================注意这里的语法!!不能写res.add(path);==================
33+
res.add( new ArrayList(path) );
34+
return;
35+
}
36+
// 如果枚举到最后一个数,但是还没有组合target,直接返回
37+
if( u == cs.length ) return ;
38+
39+
//枚举。可以选0个,但是条件是不要超过target
40+
for( int i = 0 ; cs[u] * i <= target ; i++ ){
41+
// 第一次选0个不加,直接到下一层
42+
dfs(cs , u + 1 , target - cs[u] * i );
43+
// 第二次及以后选择了有数字,加到方案中.所以在递归之后加入
44+
path.add(cs[u]);
45+
}
46+
47+
// 恢复现场:清空path
48+
for( int i = 0 ; cs[u] * i <= target ; i++ ){
49+
path.remove( path.size() - 1 );
50+
}
51+
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetCode.subject.number1_50;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-07-31-23:31
8+
* @Function Description :42. 接雨水
9+
*/
10+
public class _42RainWater {
11+
class Solution {
12+
public int trap(int[] height) {
13+
// 栈存高度的下标
14+
Stack<Integer> stk = new Stack<>();
15+
int res = 0;
16+
// 枚举所有柱子的高度
17+
for(int i = 0 ; i < height.length ; i++ ){
18+
// 记录上一个柱子的高度,第一次0
19+
int last = 0;
20+
// 栈非空并且栈顶元素小于等于上一个高度
21+
while( stk.size() > 0 && height[stk.peek()] <= height[i] ) {
22+
// 先加上最底下的一部分凹糟的面试
23+
res += ( height[stk.peek()] - last ) * ( i - stk.peek() - 1 );
24+
// 更新成大凹糟的下标
25+
last = height[stk.peek()];
26+
// 计算完当前凹糟后,弹出
27+
stk.pop();
28+
}
29+
// 宽度 * 高度
30+
if( stk.size() > 0 ) res += (i - stk.peek() - 1 ) * (height[i] - last);
31+
// 当前元素入栈
32+
stk.push(i);
33+
}
34+
35+
return res;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)