Skip to content

Commit 0d34cee

Browse files
author
wb-hjk570755
committed
condition用例 三个线程交替打印
1 parent 4081e2b commit 0d34cee

File tree

6 files changed

+415
-0
lines changed

6 files changed

+415
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/9/28
10+
*/
11+
public class ArrHeaderTailMax {
12+
13+
public static int max(int[] a){
14+
int sum = 0;
15+
for(int i=0;i<a.length;i++){
16+
sum = sum + a[i];
17+
}
18+
System.out.println("sum="+sum);
19+
20+
return maxDetail(a,sum,0,a.length-1,0);
21+
}
22+
23+
public static int maxDetail(int[] a,int sum,int leftIndex,int rightIndex,int max){
24+
int chooseNum = 0;
25+
if(rightIndex-leftIndex>1){
26+
int leftMax = sum - maxDetail(a,sum-a[leftIndex],leftIndex+1,rightIndex,max);
27+
int rightMax = sum - maxDetail(a,sum-a[rightIndex],leftIndex,rightIndex-1,max);
28+
29+
chooseNum = Math.max(leftMax,rightMax);
30+
return chooseNum;
31+
}else {
32+
return Math.max(a[leftIndex],a[rightIndex]);
33+
}
34+
}
35+
36+
public static void main(String[] args) {
37+
int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
38+
int n = max(v);
39+
System.out.println(n);
40+
41+
System.out.println(max2_dync(v));
42+
}
43+
44+
/**
45+
*
46+
*
47+
* dp[i][j]=sum[i,j]-min(dp[i-1][j],dp[i][j-1])
48+
* @param b
49+
* @return
50+
*/
51+
public static int max2_dync(int[] b){
52+
int n = b.length;
53+
int[] a=new int[n+1];
54+
int[][] dp=new int[n+1][n+1];
55+
int[] sum=new int[n+1];
56+
for(int i=1;i<=n;i++){
57+
a[i]=b[i-1];
58+
sum[i]=sum[i-1]+a[i];
59+
dp[i][i]=a[i];
60+
}
61+
62+
for(int i=n;i>0;i--){
63+
for(int j=i+1;j<=n;j++){
64+
dp[i][j]=sum[j]-sum[i-1]-Math.min(dp[i+1][j],dp[i][j-1]);
65+
}
66+
}
67+
int first=dp[1][n];
68+
int second=sum[n]-first;
69+
System.out.println(first+" "+second);
70+
return first;
71+
72+
}
73+
74+
75+
76+
77+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/9/23
10+
*/
11+
public class ClimbStairs {
12+
public static void main(String[] args) {
13+
ClimbStairs climbStairs = new ClimbStairs();
14+
System.out.println(System.currentTimeMillis());
15+
System.out.println(climbStairs.climbStairs(46));
16+
System.out.println(System.currentTimeMillis());
17+
18+
System.out.println("--------------------------");
19+
20+
System.out.println(System.currentTimeMillis());
21+
System.out.println(climbStairs.climbStairs2(46));
22+
System.out.println(System.currentTimeMillis());
23+
24+
System.out.println("--------------------------");
25+
System.out.println(System.currentTimeMillis());
26+
System.out.println(climbStairs.climbStairs3(46));
27+
System.out.println(System.currentTimeMillis());
28+
}
29+
public int climbStairs(int n) {
30+
if(n==1){
31+
return 1;
32+
}
33+
if(n==2){
34+
return 2;
35+
}
36+
return climbStairs(n-1)+climbStairs(n-2);
37+
}
38+
39+
public int climbStairs3(int n) {
40+
int[] nums = new int[n];
41+
nums[0]=1;
42+
nums[1]=2;
43+
if(n==1){
44+
return 1;
45+
}
46+
if(n==2){
47+
return 2;
48+
}
49+
for (int i=2;i<n;i++){
50+
nums[i]=nums[i-1]+nums[i-2];
51+
}
52+
return nums[n-1];
53+
}
54+
55+
public int climbStairs2(int n) {
56+
if(n==1){
57+
return 1;
58+
}
59+
int[] nums = new int[n];
60+
nums[0] = 1;
61+
nums[1] = 2;
62+
for (int i=2;i<n;i++){
63+
nums[i] = nums[i-2] + nums[i-1];
64+
}
65+
return nums[n-1];
66+
}
67+
}

src/com/blankj/myself/Exercise14.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/9/28
10+
*/
11+
public class Exercise14 {
12+
static int max(int[] a, int i, int j) {
13+
int sum = 0;
14+
while (i <= j) {
15+
sum += a[i];
16+
i++;
17+
}
18+
return act(a, sum, 0, a.length - 1);
19+
}
20+
21+
private static int act(int[] a, int sum, int l, int r) {
22+
int lmax, rmax;
23+
if (r - l > 1) {
24+
// 意思是先选一个,然后求剩下数组对方先选获得的最大值,用sum减去对方获得的最大值,即是自己的最大值
25+
// 然后比较选头还是尾更好
26+
lmax = sum - act(a, sum - a[l], l + 1, r);
27+
rmax = sum - act(a, sum - a[r], l, r - 1);
28+
return lmax > rmax ? lmax : rmax;
29+
} else {//剩下2个
30+
return a[l] > a[r] ? a[l] : a[r];
31+
}
32+
33+
}
34+
35+
public static void main(String[] args) {
36+
int v[] = {1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2};
37+
int n = max(v, 0, v.length - 1);
38+
System.out.println(n);
39+
}
40+
}

src/com/blankj/myself/StackTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.blankj.myself;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/9/23
12+
*/
13+
public class StackTest {
14+
public static void main(String[] args) {
15+
StackTest stackTest = new StackTest();
16+
stackTest.isValid("()");
17+
}
18+
public boolean isValid(String s) {
19+
Stack<Character> stack = new Stack<>();
20+
for(char a:s.toCharArray()){
21+
if(a=='('){
22+
stack.push(')');
23+
}else if(a=='['){
24+
stack.push(']');
25+
}else if(a=='{'){
26+
stack.push('}');
27+
}else {
28+
if(stack.pop()!=a||stack.isEmpty()){
29+
return false;
30+
}
31+
}
32+
}
33+
return stack.isEmpty();
34+
}
35+
}
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.blankj.myself;
2+
3+
import java.util.concurrent.atomic.AtomicInteger;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.ReentrantLock;
6+
7+
8+
/**
9+
* Description:
10+
* Copyright: Copyright (c) 2012
11+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
12+
*
13+
* @author huangjk
14+
* @version 1.0 2020/9/28
15+
*/
16+
public class ThreeThreadPrint {
17+
18+
public static void main(String[] args) throws InterruptedException {
19+
String a = "sahjfhasjfhakjfhkjh";
20+
char[] arry = a.toCharArray();
21+
AtomicInteger index = new AtomicInteger();
22+
ReentrantLock lock = new ReentrantLock();
23+
Condition condition1 = lock.newCondition();
24+
Condition condition2 = lock.newCondition();
25+
Condition condition3 = lock.newCondition();
26+
27+
28+
new Thread(()->{
29+
Boolean finish = false;
30+
31+
try {
32+
33+
lock.lock();
34+
while (!finish) {
35+
36+
System.out.println(Thread.currentThread().getName() + ": " + arry[index.getAndAdd(1)]);
37+
condition2.signal();
38+
condition1.await();
39+
40+
if (index.get() > arry.length) {
41+
finish = true;
42+
}
43+
}
44+
45+
}catch (Exception e){
46+
47+
}finally {
48+
lock.unlock();
49+
}
50+
51+
}).start();
52+
53+
54+
new Thread(()->{
55+
Boolean finish = false;
56+
57+
try {
58+
lock.lock();
59+
while (!finish) {
60+
System.out.println(Thread.currentThread().getName() + ": " + arry[index.getAndAdd(1)]);
61+
62+
condition3.signal();
63+
condition2.await();
64+
65+
if (index.get() > arry.length) {
66+
finish = true;
67+
}
68+
}
69+
70+
}catch (Exception e){
71+
72+
}finally {
73+
lock.unlock();
74+
}
75+
76+
77+
78+
}).start();
79+
80+
new Thread(()->{
81+
Boolean finish = false;
82+
83+
try {
84+
lock.lock();
85+
while (!finish) {
86+
System.out.println(Thread.currentThread().getName() + ": " + arry[index.getAndAdd(1)]);
87+
88+
condition1.signal();
89+
condition3.await();
90+
91+
if (index.get() > arry.length) {
92+
finish = true;
93+
}
94+
}
95+
96+
}catch (Exception e){
97+
98+
}finally {
99+
lock.unlock();
100+
}
101+
102+
103+
104+
}).start();
105+
106+
107+
Thread.sleep(100000);
108+
}
109+
110+
}

0 commit comments

Comments
 (0)