Skip to content

Commit caba2c5

Browse files
authored
Merge pull request doocs#222 from huaxu1024/master
Create Solution.java
2 parents 841857d + 0e1ff5e commit caba2c5

File tree

10 files changed

+299
-0
lines changed

10 files changed

+299
-0
lines changed

solution/0148.Sort List/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Sort List
2+
3+
Sort a linked list in O(n log n) time using constant space complexity.
4+
5+
6+
## Example 1:
7+
```
8+
Input: 4->2->1->3
9+
Output: 1->2->3->4
10+
```
11+
12+
## Example 2:
13+
```
14+
Input: -1->5->3->4->0
15+
Output: -1->5->3->4->0
16+
```
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode sortList(ListNode head) {
11+
if (head == null || head.next == null)
12+
return head;
13+
// get middle index node
14+
ListNode fast = head.next, slow = head;
15+
while (fast != null && fast.next != null) {
16+
slow = slow.next;
17+
fast = fast.next.next;
18+
}
19+
// cut
20+
ListNode tmp = slow.next;
21+
slow.next = null;
22+
ListNode left = sortList(head);
23+
ListNode right = sortList(tmp);
24+
ListNode h = new ListNode(0);
25+
ListNode res = h;
26+
// merge
27+
while (left != null && right != null) {
28+
if (left.val < right.val) {
29+
h.next = left;
30+
left = left.next;
31+
} else {
32+
h.next = right;
33+
right = right.next;
34+
}
35+
h = h.next;
36+
}
37+
h.next = left != null ? left : right;
38+
return res.next;
39+
}
40+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*
9+
* O(nlogn) ==》 time complexity
10+
* O(n) ==》space complexity
11+
*/
12+
class Solution {
13+
14+
public ListNode sortList(ListNode head) {
15+
List<Integer> numList = new ArrayList<>();
16+
while (head != null) {
17+
numList.add(head.val);
18+
head = head.next;
19+
}
20+
Collections.sort(numList);
21+
22+
head = new ListNode(0);
23+
ListNode res = head;
24+
for (Integer num : numList) {
25+
head.next = new ListNode(num);
26+
head = head.next;
27+
}
28+
return res.next;
29+
}
30+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Print in Order
2+
3+
Suppose we have a class:
4+
```
5+
public class Foo {
6+
  public void first() { print("first"); }
7+
  public void second() { print("second"); }
8+
  public void third() { print("third"); }
9+
}
10+
```
11+
The same instance of `Foo` will be passed to three different threads.
12+
13+
- Thread A will call `first()`.
14+
- Thread B will call `second()`.
15+
- Thread C will call `third()`.
16+
17+
Design a mechanism and modify the program to ensure that `second()` is executed after `first()`, and `third()` is executed after second().
18+
19+
## Example 1:
20+
```
21+
Input: [1,2,3]
22+
Output: "firstsecondthird"
23+
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output.
24+
```
25+
26+
## Example 2:
27+
```
28+
Input: [1,3,2]
29+
Output: "firstsecondthird"
30+
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output.
31+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.concurrent.CountDownLatch;
2+
3+
class Foo {
4+
private CountDownLatch one = new CountDownLatch(1);
5+
private CountDownLatch two = new CountDownLatch(1);
6+
public Foo() {}
7+
8+
public void first(Runnable printFirst) throws InterruptedException {
9+
printFirst.run();
10+
one.countDown();
11+
}
12+
13+
public void second(Runnable printSecond) throws InterruptedException {
14+
one.await();
15+
printSecond.run();
16+
two.countDown();
17+
}
18+
19+
public void third(Runnable printThird) throws InterruptedException {
20+
two.await();
21+
printThird.run();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Foo {
2+
3+
private volatile int index = 1;
4+
public Foo() {}
5+
6+
public void first(Runnable printFirst) throws InterruptedException {
7+
while (index != 1) {}
8+
printFirst.run();
9+
index = 2;
10+
}
11+
12+
public void second(Runnable printSecond) throws InterruptedException {
13+
while (index != 2) {}
14+
printSecond.run();
15+
index = 3;
16+
}
17+
18+
public void third(Runnable printThird) throws InterruptedException {
19+
while (index != 3) {}
20+
printThird.run();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Print FooBar Alternately
2+
3+
Suppose you are given the following code:
4+
```
5+
class FooBar {
6+
public void foo() {
7+
    for (int i = 0; i < n; i++) {
8+
      print("foo");
9+
  }
10+
}
11+
12+
public void bar() {
13+
    for (int i = 0; i < n; i++) {
14+
      print("bar");
15+
    }
16+
}
17+
}
18+
```
19+
The same instance of `FooBar` will be passed to two different threads. Thread A will call `foo()` while thread B will call `bar()`. Modify the given program to output "**foobar**" n times.
20+
21+
## Example 1:
22+
```
23+
Input: n = 1
24+
Output: "foobar"
25+
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). "foobar" is being output 1 time.
26+
```
27+
28+
## Example 2:
29+
```
30+
Input: n = 2
31+
Output: "foobarfoobar"
32+
Explanation: "foobar" is being output 2 times.
33+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.concurrent.locks.Lock;
2+
import java.util.concurrent.locks.ReentrantLock;
3+
4+
5+
class FooBar {
6+
7+
private int n;
8+
private Lock lock;
9+
private volatile Boolean flag;
10+
11+
public FooBar(int n) {
12+
this.n = n;
13+
this.flag = true;
14+
this.lock = new ReentrantLock(true);
15+
}
16+
17+
public void foo(Runnable printFoo) throws InterruptedException {
18+
for (int i = 0; i < n;) {
19+
lock.lock();
20+
if (flag) {
21+
printFoo.run();
22+
flag = false;
23+
i++;
24+
}
25+
lock.unlock();
26+
}
27+
}
28+
29+
public void bar(Runnable printBar) throws InterruptedException {
30+
for (int i = 0; i < n;) {
31+
lock.lock();
32+
if (!flag) {
33+
printBar.run();
34+
flag = true;
35+
i++;
36+
}
37+
lock.unlock();
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.concurrent.Semaphore;
2+
3+
4+
class FooBar {
5+
6+
private int n;
7+
private Semaphore semaphoreA;
8+
private Semaphore semaphoreB;
9+
10+
public FooBar(int n) {
11+
this.n = n;
12+
this.semaphoreA = new Semaphore(1);
13+
this.semaphoreB = new Semaphore(0);
14+
}
15+
16+
public void foo(Runnable printFoo) throws InterruptedException {
17+
for (int i = 0; i < n; i++) {
18+
semaphoreA.acquire();
19+
printFoo.run();
20+
semaphoreB.release();
21+
}
22+
}
23+
24+
public void bar(Runnable printBar) throws InterruptedException {
25+
for (int i = 0; i < n; i++) {
26+
semaphoreB.acquire();
27+
printBar.run();
28+
semaphoreA.release();
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.concurrent.CountDownLatch;
2+
3+
4+
class FooBar {
5+
6+
private int n;
7+
private CountDownLatch countDownLatchA;
8+
private CountDownLatch countDownLatchB;
9+
10+
public FooBar(int n) {
11+
this.n = n;
12+
this.countDownLatchA = new CountDownLatch(1);
13+
this.countDownLatchB = new CountDownLatch(1);
14+
}
15+
16+
public void foo(Runnable printFoo) throws InterruptedException {
17+
for (int i = 0; i < n; i++) {
18+
countDownLatchA.await();
19+
printFoo.run();
20+
countDownLatchB.countDown();
21+
countDownLatchA = new CountDownLatch(1);
22+
}
23+
}
24+
25+
public void bar(Runnable printBar) throws InterruptedException {
26+
for (int i = 0; i < n; i++) {
27+
countDownLatchA.countDown();
28+
countDownLatchB.await();
29+
printBar.run();
30+
countDownLatchB = new CountDownLatch(1);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)