Skip to content

Commit 1549e40

Browse files
committed
ok
1 parent e683058 commit 1549e40

File tree

4 files changed

+310
-190
lines changed

4 files changed

+310
-190
lines changed

src/com/leetcode/Main226.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.leetcode;
2+
3+
/**
4+
* 226. 翻转二叉树
5+
* 翻转一棵二叉树。
6+
*
7+
* 示例:
8+
*
9+
* 输入:
10+
*
11+
* 4
12+
* / \
13+
* 2 7
14+
* / \ / \
15+
* 1 3 6 9
16+
* 输出:
17+
*
18+
* 4
19+
* / \
20+
* 7 2
21+
* / \ / \
22+
* 9 6 3 1
23+
*/
24+
public class Main226 {
25+
public class TreeNode {
26+
int val;
27+
TreeNode left;
28+
TreeNode right;
29+
TreeNode(int x) { val = x; }
30+
}
31+
public TreeNode invertTree(TreeNode root) {
32+
if (root == null) {
33+
return null;
34+
}
35+
TreeNode left = invertTree(root.left);
36+
TreeNode right = invertTree(root.right);
37+
root.left = right;
38+
root.right = left;
39+
return root;
40+
}
41+
}

src/com/leetcode/Main674.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.leetcode;
2+
3+
/**
4+
* 674. 最长连续递增序列
5+
* 给定一个未经排序的整数数组,找到最长且连续的的递增序列,并返回该序列的长度。
6+
* 示例 1:
7+
*
8+
* 输入: [1,3,5,4,7]
9+
* 输出: 3
10+
* 解释: 最长连续递增序列是 [1,3,5], 长度为3。
11+
* 尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。
12+
*/
13+
public class Main674 {
14+
public int findLengthOfLCIS(int[] nums) {
15+
if (nums == null || nums.length == 0)
16+
return 0;
17+
if (nums.length == 1)
18+
return 1;
19+
int left = 0;
20+
int right = 1;
21+
int res = Integer.MIN_VALUE;
22+
while (right < nums.length) {
23+
int max = 1;
24+
while (right < nums.length && nums[left] < nums[right]) {
25+
max += 1;
26+
left += 1;
27+
right += 1;
28+
}
29+
if (max > res)
30+
res = max;
31+
left = right;
32+
right += 1;
33+
}
34+
return res;
35+
}
36+
}

src/com/leetcode/ThreadTest.java

Lines changed: 23 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,43 @@
11
package com.leetcode;
22

3-
import java.util.concurrent.*;
4-
53
public class ThreadTest {
6-
/////////////////生产者消费者模型////////////////////////////////////////////////////////
7-
public static class Goods {
8-
private String name;
9-
Goods(String name) {
10-
this.name = name;
11-
}
12-
}
13-
public static class Producer implements Runnable{
14-
private BlockingQueue<Goods> queue;
15-
Producer(BlockingQueue<Goods> queue) {
16-
this.queue = queue;
17-
}
18-
@Override
19-
public void run() {
20-
while (true) {
21-
Goods good = new Goods("包子");
22-
try {
23-
Thread.sleep(1000);
24-
queue.put(good);
25-
System.out.println(Thread.currentThread().getName() + "生产完成");
26-
} catch(Exception e) {
27-
e.printStackTrace();
28-
}
29-
}
30-
}
31-
}
32-
public static class Consumer implements Runnable {
33-
private BlockingQueue<Goods> queue;
34-
Consumer(BlockingQueue<Goods> queue) {
35-
this.queue = queue;
36-
}
37-
@Override
38-
public void run() {
39-
while (true) {
40-
try {
41-
Thread.sleep(1000);
42-
Goods good = queue.take();
43-
System.out.println(Thread.currentThread().getName() + "消费完成");
44-
} catch (Exception e) {
45-
e.printStackTrace();
46-
}
47-
}
48-
}
49-
}
50-
/////////////////生产者消费者模型////////////////////////////////////////////////////////
51-
/////////////////多线程打印奇数和偶数/////////////////////////////////////////////////////
52-
public static class Number {
53-
int num;
54-
Number(int num) {
4+
public static class Thread3Print implements Runnable{
5+
private static Object lock = new Object();
6+
private static int count = 0;
7+
private int num;
8+
public Thread3Print(int num) {
559
this.num = num;
5610
}
57-
}
58-
public static class EvenPrinter implements Runnable {
59-
Number number;
60-
EvenPrinter(Number number) {
61-
this.number = number;
62-
}
6311
@Override
6412
public void run() {
65-
while (number.num <= 100) {
66-
// if (Thread.currentThread().isInterrupted())
67-
// return;
68-
synchronized(number) {
69-
try {
70-
Thread.sleep(100);
71-
} catch (InterruptedException e) {
72-
e.printStackTrace();
73-
}
74-
if (number.num % 2 == 0) {
75-
System.out.println(Thread.currentThread().getName() + "打印:" + number.num);
76-
number.num += 1;
77-
number.notify();
78-
}
79-
else {
13+
synchronized (lock) {
14+
while (count <= 100) {
15+
if (count % 3 == num) {
16+
System.out.println(Thread.currentThread().getName() + "打印" + count);
17+
count += 1;
18+
lock.notifyAll();
19+
} else {
8020
try {
81-
number.wait();
82-
} catch (Exception e) {
83-
e.printStackTrace();
84-
}
85-
}
86-
}
87-
}
88-
}
89-
}
90-
public static class OddPrinter implements Runnable {
91-
Number number;
92-
OddPrinter(Number number) {
93-
this.number = number;
94-
}
95-
@Override
96-
public void run() {
97-
while (number.num <= 100) {
98-
// if (Thread.currentThread().isInterrupted())
99-
// return;
100-
synchronized(number) {
101-
if (number.num % 2 != 0) {
102-
try {
103-
Thread.sleep(100);
21+
lock.wait();
10422
} catch (InterruptedException e) {
10523
e.printStackTrace();
10624
}
107-
System.out.println(Thread.currentThread().getName() + "打印:" + number.num);
108-
number.num += 1;
109-
number.notify();
110-
}
111-
else {
112-
try {
113-
number.wait();
114-
} catch (Exception e) {
115-
e.printStackTrace();
116-
}
11725
}
11826
}
11927
}
12028
}
12129
}
122-
public static void main(String[] args) {
123-
// BlockingQueue<Goods> queue = new ArrayBlockingQueue<>(5);
124-
// Producer producer = new Producer(queue);
125-
// Consumer consumer = new Consumer(queue);
126-
// for (int i = 0; i < 2; i++) {
127-
// new Thread(producer, "线程"+i).start();
128-
// }
129-
// for (int i = 0; i < 2; i++) {
130-
// new Thread(consumer, "线程"+i).start();
131-
// }
132-
133-
// Number number = new Number(0);
134-
// EvenPrinter evenPrinter = new EvenPrinter(number);
135-
// OddPrinter oddPrinter = new OddPrinter(number);
136-
// Thread evenThread = new Thread(evenPrinter, "偶数打印线程");
137-
// Thread oddThread = new Thread(oddPrinter, "奇数打印线程");
138-
// evenThread.start();
139-
// oddThread.start();
140-
141-
142-
143-
// //创建一个可缓存线程池
144-
// ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
145-
// for (int i = 0; i < 10; i++) {
146-
// try {
147-
// // sleep可明显看到使用的是线程池里面以前的线程,没有创建新的线程
148-
// Thread.sleep(1000);
149-
// } catch (InterruptedException e) {
150-
// e.printStackTrace();
151-
// }
152-
// cachedThreadPool.execute(new Runnable() {
153-
// public void run() {
154-
// // 打印正在执行的缓存线程信息
155-
// System.out.println(Thread.currentThread().getName()
156-
// + "正在被执行");
157-
// try {
158-
// Thread.sleep(1000);
159-
// } catch (InterruptedException e) {
160-
// e.printStackTrace();
161-
// }
162-
// }
163-
// });
164-
// }
165-
166-
// //定长线程池
167-
// ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
168-
// for (int i = 0; i < 10; i++) {
169-
// fixedThreadPool.execute(new Runnable() {
170-
// public void run() {
171-
// try {
172-
// // 打印正在执行的缓存线程信息
173-
// System.out.println(Thread.currentThread().getName()
174-
// + "正在被执行");
175-
// Thread.sleep(2000);
176-
// } catch (InterruptedException e) {
177-
// e.printStackTrace();
178-
// }
179-
// }
180-
// });
181-
// }
182-
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
183-
//延迟1秒执行
184-
/*scheduledThreadPool.schedule(new Runnable() {
185-
public void run() {
186-
System.out.println("延迟1秒执行");
187-
}
188-
}, 1, TimeUnit.SECONDS);*/
189-
190-
191-
//延迟1秒后每3秒执行一次
192-
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
193-
public void run() {
194-
System.out.println("延迟1秒后每3秒执行一次");
195-
}
196-
}, 1, 3, TimeUnit.SECONDS);
197-
198-
199-
20030

201-
// try {
202-
// Thread.sleep(2000);
203-
// } catch (InterruptedException e) {
204-
// e.printStackTrace();
205-
// }
206-
// evenThread.interrupt();
207-
// oddThread.interrupt();
31+
public static void main(String[] args) throws InterruptedException {
32+
Thread thread0 = new Thread(new Thread3Print(0), "线程0");
33+
Thread thread1 = new Thread(new Thread3Print(1), "线程1");
34+
Thread thread2 = new Thread(new Thread3Print(2), "线程2");
35+
thread0.start();
36+
thread1.start();
37+
thread2.start();
38+
thread0.join();
39+
thread1.join();
40+
thread2.join();
20841

20942
}
21043
}

0 commit comments

Comments
 (0)