Skip to content

Commit 11346d2

Browse files
committed
ok
1 parent 65634b4 commit 11346d2

File tree

5 files changed

+207
-19
lines changed

5 files changed

+207
-19
lines changed

src/com/leetcode/Main135.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Hashtable;
6+
7+
/**
8+
* 135.分发糖果
9+
* 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。
10+
*
11+
* 你需要按照以下要求,帮助老师给这些孩子分发糖果:
12+
*
13+
* 每个孩子至少分配到 1 个糖果。
14+
* 相邻的孩子中,评分高的孩子必须获得更多的糖果。
15+
* 那么这样下来,老师至少需要准备多少颗糖果呢?
16+
*
17+
* 示例 1:
18+
*
19+
* 输入: [1,0,2]
20+
* 输出: 5
21+
* 解释: 你可以分别给这三个孩子分发 2、1、2 颗糖果。
22+
* 示例 2:
23+
*
24+
* 输入: [1,2,2]
25+
* 输出: 4
26+
* 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。
27+
* 第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
28+
*/
29+
public class Main135 {
30+
public int candy(int[] ratings) {
31+
int n = ratings.length;
32+
int[] tang = new int[n];
33+
Arrays.fill(tang, 1);
34+
for (int i = 1; i < n; i++) {
35+
if (ratings[i] > ratings[i - 1]) {
36+
tang[i] = tang[i-1] + 1;
37+
}
38+
}
39+
for (int i = n-2; i >= 0; i--) {
40+
if (ratings[i] > ratings[i + 1]) {
41+
tang[i] = Math.max(tang[i], tang[i + 1] + 1);
42+
}
43+
}
44+
int ans = 0;
45+
for (int num: tang) {
46+
ans += num;
47+
}
48+
return ans;
49+
}
50+
}

src/com/leetcode/Sort.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ public void xuanzeSort(int[] nums) {
3737

3838
/***************************************插入排序******************************************
3939
* 平均时间复杂度O(n^2) 空间复杂度O(1) 稳定 最差时间复杂度O(n^2) 最好时间复杂度O(n) */
40-
public void charuSort(int[] nums) {
41-
int n = nums.length;
42-
for (int i = 1; i < n; i++) {
43-
int k = i-1;
44-
int tmp = nums[i];
45-
while (k >=0 && nums[k] > tmp)
46-
k--;
47-
for (int j = i; j > k + 1; j--)
48-
nums[j] = nums[j-1];
49-
nums[k+1] = tmp;
40+
public void charuSort(int[] array) {
41+
if (array.length == 0)
42+
return;
43+
int current;
44+
for (int i = 0; i < array.length - 1; i++) {
45+
current = array[i + 1];
46+
int preIndex = i;
47+
while (preIndex >= 0 && current < array[preIndex])
48+
{
49+
array[preIndex + 1] = array[preIndex];
50+
preIndex--;
51+
}
52+
array[preIndex + 1] = current;
5053
}
5154
}
5255
/***************************************插入排序******************************************/

src/com/leetcode/Test.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+
import java.util.concurrent.*;
4+
5+
public class Test {
6+
public static void main(String[] args) {
7+
ExecutorService executor = Executors.newCachedThreadPool();
8+
Task task = new Task();
9+
Future<Integer> result = executor.submit(task);
10+
executor.shutdown();
11+
12+
try {
13+
Thread.sleep(1000);
14+
} catch (InterruptedException e1) {
15+
e1.printStackTrace();
16+
}
17+
18+
System.out.println("主线程在执行任务");
19+
20+
try {
21+
System.out.println("task运行结果"+result.get());
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
} catch (ExecutionException e) {
25+
e.printStackTrace();
26+
}
27+
28+
System.out.println("所有任务执行完毕");
29+
}
30+
}
31+
class Task implements Callable<Integer> {
32+
@Override
33+
public Integer call() throws Exception {
34+
System.out.println("子线程在进行计算");
35+
Thread.sleep(3000);
36+
int sum = 0;
37+
for(int i=0;i<100;i++)
38+
sum += i;
39+
return sum;
40+
}
41+
}

src/com/leetcode/ThreadPool.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.leetcode;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
public class ThreadPool {
7+
public void func1() {
8+
Object object = new Object();
9+
for (int i = 0; i < 10; i++) {
10+
synchronized (ThreadPool.class) {
11+
if (object == null) {
12+
try {
13+
object = new Object();
14+
i -= 1;
15+
this.wait();
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
} else {
20+
System.out.print(i + " ");
21+
object = null;
22+
this.notify();
23+
}
24+
}
25+
}
26+
}
27+
public static void main(String[] args) {
28+
ThreadPool t1 = new ThreadPool();
29+
ThreadPool t2 = new ThreadPool();
30+
ExecutorService executorService = Executors.newCachedThreadPool();
31+
executorService.execute(() -> t1.func1());
32+
executorService.execute(() -> t2.func1());
33+
executorService.shutdown();
34+
}
35+
}

src/com/leetcode/ThreadTest.java

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.leetcode;
22

3-
import java.util.concurrent.ArrayBlockingQueue;
4-
import java.util.concurrent.BlockingQueue;
3+
import java.util.concurrent.*;
54

65
public class ThreadTest {
76
/////////////////生产者消费者模型////////////////////////////////////////////////////////
@@ -131,13 +130,73 @@ public static void main(String[] args) {
131130
// new Thread(consumer, "线程"+i).start();
132131
// }
133132

134-
Number number = new Number(0);
135-
EvenPrinter evenPrinter = new EvenPrinter(number);
136-
OddPrinter oddPrinter = new OddPrinter(number);
137-
Thread evenThread = new Thread(evenPrinter, "偶数打印线程");
138-
Thread oddThread = new Thread(oddPrinter, "奇数打印线程");
139-
evenThread.start();
140-
oddThread.start();
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+
141200

142201
// try {
143202
// Thread.sleep(2000);

0 commit comments

Comments
 (0)