Skip to content

Commit ba206f4

Browse files
committed
add leetcode: 1114,1115,32
1 parent 2d67a5b commit ba206f4

File tree

9 files changed

+168
-15
lines changed

9 files changed

+168
-15
lines changed

Algorithm/src/acwing/basic/chapter2/_240FoodChain.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public class _240FoodChain {
7171
1.初始化时,每个点就是一个单独的集合,所以d距离就是到自己的距离0;
7272
2.然后根据输入的每句话会进行合并和对假话的判断,最终所有动物都会在一个集合中,判断每个动物属于哪
7373
一类,就是看模3之后的余数。
74-
3.对于每一句话有两种情况,同类或者不同类;对于每种情况,都需要判断他们在一个集合中假话的情况、不在一个集合中合并的处理。
74+
3.对于每一句话有两种情况,同类或者不同类;对于每种情况,都需要判断他们在一个集合中假话的情况、不在一个集合
75+
中合并的处理。
7576
(重点!)
7677
*/
7778
public static int N = 50010;
@@ -105,8 +106,9 @@ public static void main(String[] args) throws IOException {
105106
d[x] % 3 != d[y] % 3.
106107
*/
107108
if (px == py && (d[x] - d[y]) % 3 != 0) res++;
108-
else if (px != py) {//这里一定要写上判断px和py不是一个集合,不然也有可能出现情况,如上面if的第二种情况
109-
// xy不是一个集合,把他们合并到一个集合中,x的这个集合的父结点更新为y集合的根结点
109+
//这里一定要写上判断px和py不是一个集合,不然也有可能出现情况,如上面if的第二种情况
110+
else if (px != py) {
111+
//同类;但 xy不是一个集合,把他们合并到一个集合中(x合并到y集合中),x的这个集合的父结点更新为y集合的根结点
110112
p[px] = py;
111113
/*xy是同类,x这个集合合并到y集合中时,x到本集合根的距离加上本集合到y集合根的距离之后的总和
112114
模3之后是0。也就是d【x】+ ?模3的余数跟d【y】模3的余数相等,进一步优化就是(d【x】+?-d【y】)
@@ -133,7 +135,7 @@ else if (px != py) {
133135
// 返回x结点所属的集合,根
134136
public static int find(int x) {
135137
if (p[x] != x) {
136-
//存储x的根是谁。如果不存就是p[x]=find(p[x]);x的父结点p[x]就会被更改,下面计算距离就会出错。
138+
//存储x的根是谁。如果不存就是p[x]=find(p[x]);下一步:x的父结点p[x]就会被更改,下面计算距离就会出错。
137139
int t = find(p[x]);
138140
// x到根结点的距离:自己到父结点的距离加上父结点到根的距离
139141
d[x] += d[p[x]];

Algorithm/src/acwing/basic/chapter2/_837NumberOfConnectedBlocks.java

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static void main(String[] args) throws IOException {
7070
if (find(a) == find(b)) continue;
7171
// 把a合并到b中,同时更新b集合的数量
7272
cnt[find(b)] += cnt[find(a)];
73+
// a集合的根结点的父亲 是 b集合的根结点
7374
p[find(a)] = find(b);
7475
} else if (str[0].equals("Q1")) {
7576
// 查找两个集合是否在一个集合中
@@ -87,7 +88,9 @@ public static void main(String[] args) throws IOException {
8788

8889
// 查找一个数属于哪个集合,路径压缩:一旦查找到其根结点这条路上所有都指向根
8990
public static int find(int x) {
91+
// p[x]!=x。说明x不是自己这个集合的根,然后就继续从他的父结点p[x]开始遍历递归find(p[x])
9092
if (p[x] != x) p[x] = find(p[x]);
93+
// 如果p[x]=x,说明已经遍历到这个集合的最底层了,也就是集合的根,这个时候返回p[x]即可
9194
return p[x];
9295
}
9396
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetCode.multiThread;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-29-16:56
6+
* @Function Description :1114.按序打印
7+
*
8+
*/
9+
public class _1114PrintInOrder {
10+
class Foo {
11+
12+
private boolean firstFinished = false;
13+
private boolean secondFinished = false;
14+
private Object lock = new Object();
15+
16+
public Foo() {
17+
18+
}
19+
20+
public void first(Runnable printFirst) throws InterruptedException {
21+
synchronized(lock){
22+
printFirst.run();
23+
firstFinished = true;
24+
lock.notifyAll();
25+
}
26+
// printFirst.run() outputs "first". Do not change or remove this line.
27+
28+
}
29+
30+
public void second(Runnable printSecond) throws InterruptedException {
31+
synchronized( lock ){
32+
while( !firstFinished ){
33+
lock.wait();
34+
}
35+
printSecond.run();
36+
secondFinished = true;
37+
lock.notifyAll();
38+
}
39+
// printSecond.run() outputs "second". Do not change or remove this line.
40+
41+
}
42+
43+
public void third(Runnable printThird) throws InterruptedException {
44+
synchronized(lock){
45+
while( !secondFinished ){
46+
lock.wait();
47+
}
48+
printThird.run();
49+
}
50+
// printThird.run() outputs "third". Do not change or remove this line.
51+
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetCode.multiThread;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-29-16:57
6+
* @Function Description :1115.交替打印 FooBar
7+
*/
8+
public class _1115PrintFoobarAlternately {
9+
class FooBar {
10+
private int n;
11+
private boolean finished = false;
12+
private Object lock = new Object();
13+
14+
15+
public FooBar(int n) {
16+
this.n = n;
17+
}
18+
19+
public void foo(Runnable printFoo) throws InterruptedException {
20+
for (int i = 0; i < n; i++) {
21+
// printFoo.run() outputs "foo". Do not change or remove this line.
22+
synchronized(lock){
23+
while( finished ){
24+
lock.wait();
25+
}
26+
printFoo.run();
27+
finished = true;
28+
lock.notifyAll();
29+
// System.out.println( "111" );
30+
}
31+
}
32+
}
33+
34+
public void bar(Runnable printBar) throws InterruptedException {
35+
for (int i = 0; i < n; i++) {
36+
synchronized(lock){
37+
while( !finished ){
38+
lock.wait();
39+
}
40+
printBar.run();
41+
finished = false;
42+
// 需要唤醒1???
43+
lock.notifyAll();
44+
// System.out.println( "2222" );
45+
}
46+
47+
48+
}
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetCode.subject.number1_50;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-07-29-16:51
8+
* @Function Description :32.最长有效括号
9+
*/
10+
public class _32TheLongestEffectiveParentheses {
11+
class Solution {
12+
/**两个性质:
13+
1. 左右括号数相等
14+
2. 左括号数要大于等于右括号数
15+
=================(把s连续的部分划分每一段,start为每一段的起点前一个位置)===============
16+
*/
17+
// 栈里面存下标
18+
private Stack<Integer> stk = new Stack<>();
19+
20+
public int longestValidParentheses(String s) {
21+
int res = 0 ;
22+
// 栈枚举每一段连续的,其实位置是每一段的开始的前一个位置
23+
for( int i = 0 , start = -1 ; i < s.length() ; i++ ){
24+
char c = s.charAt(i);
25+
if( c == '(' ) stk.push(i);
26+
// 当前是)
27+
else{
28+
// 栈中有元素
29+
if( stk.size() > 0 ) {
30+
// 匹配一个
31+
stk.pop();
32+
// 看看栈中是否还有元素
33+
if( stk.size() > 0 ){
34+
//????
35+
res = Math.max( res , i - stk.peek() );
36+
}else{
37+
// 栈空.说明从start开始到本位段都是匹配的。一个连续的段匹配结束
38+
res = Math.max( res , i - start );
39+
}
40+
}else{
41+
// 栈空,但是来的是右括号.把遍历的段起点改为当前下标位置
42+
start = i ;
43+
}
44+
}
45+
}
46+
return res;
47+
}
48+
}
49+
}
Binary file not shown.
Binary file not shown.

pictures/image-20220728220843868.png

78.3 KB
Loading

readme.md

+5-11
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,11 @@
6464

6565
题源:剑指offer
6666

67-
| [_3FindRepeatNumber](https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/) | [_4FindNumber](https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/) |
68-
| ------------------------------------------------------------ | ------------------------------------------------------------ |
69-
| [_5ReplaceSpace](https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/) | [_6ReversePrint](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/) |
70-
| [_7BuildTree](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/) | [_9CQueue](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/) |
71-
| [_10_1Fib](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/) | [_10_2FrogJumpingSteps](https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/) |
72-
| [_11MinimumNumberOfRotationArray](https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/) | [_12PathInMatrix](https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/)矩阵中的路径 |
73-
| _13RangeOfMotionOfRobot机器人的运动范围 | |
74-
| | |
75-
| | |
76-
| | |
77-
| | |
67+
![image-20220728220843868](pictures/image-20220728220843868.png)
68+
69+
> TODO: 用y总的代码重构,有些题目还是没懂
70+
71+
7872

7973

8074

0 commit comments

Comments
 (0)