Skip to content

Commit 3103a09

Browse files
author
chenweijie
committed
算法修改
1 parent 99da757 commit 3103a09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2022
-200
lines changed

pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,40 @@
229229
</execution>
230230
</executions>
231231
</plugin>
232+
<plugin>
233+
<groupId>org.apache.maven.plugins</groupId>
234+
<artifactId>maven-enforcer-plugin</artifactId>
235+
<version>3.0.0-M3</version>
236+
<executions>
237+
<execution>
238+
<id>enforce-versions</id>
239+
<goals>
240+
<goal>enforce</goal>
241+
</goals>
242+
<configuration>
243+
<rules>
244+
<bannedPlugins>
245+
<!-- will only display a warning but does not fail the build. -->
246+
<level>WARN</level>
247+
<excludes>
248+
<exclude>org.apache.maven.plugins:maven-verifier-plugin</exclude>
249+
</excludes>
250+
<message>Please consider using the maven-invoker-plugin (http://maven.apache.org/plugins/maven-invoker-plugin/)!</message>
251+
</bannedPlugins>
252+
<requireMavenVersion>
253+
<version>2.0.6</version>
254+
</requireMavenVersion>
255+
<requireJavaVersion>
256+
<version>1.5</version>
257+
</requireJavaVersion>
258+
<requireOS>
259+
<family>unix</family>
260+
</requireOS>
261+
</rules>
262+
</configuration>
263+
</execution>
264+
</executions>
265+
</plugin>
232266
</plugins>
233267

234268
<resources>

src/main/java/com/chen/Node.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.chen;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-07-28 21:15
6+
*/
7+
public class Node {
8+
9+
10+
private Integer value;
11+
12+
private Node next;
13+
14+
15+
public Node(int val, Node next) {
16+
this.value = val;
17+
this.next = next;
18+
}
19+
20+
21+
public static void main(String[] args) {
22+
23+
//4 5 6
24+
Node a6 = new Node(6, null);
25+
Node a5 = new Node(5, a6);
26+
Node a4 = new Node(4, a5);
27+
28+
// 4 6 7 9
29+
Node b9 = new Node(9, null);
30+
Node b7 = new Node(7, b9);
31+
Node b6 = new Node(6, b7);
32+
Node b4 = new Node(4, b6);
33+
34+
35+
Node res = add(a4, b4);
36+
37+
while (res != null) {
38+
System.out.println(res.value);
39+
res = res.next;
40+
41+
}
42+
}
43+
44+
45+
public static Node add(Node node1, Node node2) {
46+
47+
Node res = new Node(-1, null);
48+
Node resNode = res;
49+
node1 = revertLink(node1);
50+
node2 = revertLink(node2);
51+
int temp = 0;
52+
while (node1 != null && node2 != null) {
53+
int val = node1.value + node2.value + temp;
54+
if (val >= 10) {
55+
temp = 1;
56+
val = val % 10;
57+
} else {
58+
temp = 0;
59+
}
60+
61+
resNode.next = new Node(val, null);
62+
node1 = node1.next;
63+
node2 = node2.next;
64+
resNode = resNode.next;
65+
}
66+
67+
68+
while (node1 != null) {
69+
int val = node1.value + temp;
70+
if (val >= 10) {
71+
temp = 1;
72+
val = val % 10;
73+
} else {
74+
temp = 0;
75+
}
76+
resNode.next = new Node(val, null);
77+
node1 = node1.next;
78+
resNode = resNode.next;
79+
}
80+
81+
while (node2 != null) {
82+
int val = node2.value + temp;
83+
if (val >= 10) {
84+
temp = 1;
85+
val = val % 10;
86+
} else {
87+
temp = 0;
88+
}
89+
resNode.next = new Node(val, null);
90+
node2 = node2.next;
91+
resNode = resNode.next;
92+
}
93+
94+
95+
return revertLink(res.next);
96+
}
97+
98+
99+
private static Node revertLink(Node node) {
100+
101+
Node pre = null;
102+
103+
while (node != null) {
104+
Node temp = node.next;
105+
node.next = pre;
106+
pre = node;
107+
node = temp;
108+
}
109+
return pre;
110+
}
111+
112+
}

src/main/java/com/chen/Test.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.chen;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* @author : chen weijie
9+
* @Date: 2020-07-28 21:38
10+
*/
11+
public class Test {
12+
13+
14+
public static void main(String[] args) {
15+
16+
int a = 0;
17+
// int b = 7;
18+
// a = a ^ b;
19+
// b = a ^ b;
20+
// a = a ^ b;
21+
22+
// a 0110
23+
// b 0111
24+
// a 0001
25+
// b 0110
26+
// a 0111
27+
28+
Stack<Integer> stack = new Stack<>();
29+
stack.pop();
30+
List<Integer> list = new ArrayList<>();
31+
System.out.println("===" + a%2);
32+
33+
34+
}
35+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.chen.algorithm;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import org.junit.Test;
5+
6+
import java.util.LinkedList;
7+
8+
/**
9+
* @author : chen weijie
10+
* @Date: 2020-08-05 21:25
11+
*/
12+
public class RevertTree {
13+
14+
15+
class TreeNode {
16+
17+
int val;
18+
TreeNode left;
19+
TreeNode right;
20+
21+
public TreeNode(int val, TreeNode left, TreeNode right) {
22+
this.right = right;
23+
this.left = left;
24+
this.val = val;
25+
}
26+
27+
public int getVal() {
28+
return val;
29+
}
30+
31+
public void setVal(int val) {
32+
this.val = val;
33+
}
34+
35+
public TreeNode getLeft() {
36+
return left;
37+
}
38+
39+
public void setLeft(TreeNode left) {
40+
this.left = left;
41+
}
42+
43+
public TreeNode getRight() {
44+
return right;
45+
}
46+
47+
public void setRight(TreeNode right) {
48+
this.right = right;
49+
}
50+
}
51+
52+
public TreeNode revert(TreeNode root) {
53+
54+
if (root == null) {
55+
return null;
56+
}
57+
58+
LinkedList<TreeNode> linkedList = new LinkedList<>();
59+
linkedList.add(root);
60+
61+
while (!linkedList.isEmpty()) {
62+
63+
int size = linkedList.size();
64+
65+
for (int i = 0; i < size; i++) {
66+
TreeNode node = linkedList.poll();
67+
if (node.left != null) {
68+
linkedList.add(node.left);
69+
}
70+
71+
if (node.right != null) {
72+
linkedList.add(node.right);
73+
}
74+
75+
TreeNode temp = node.left;
76+
node.left = node.right;
77+
node.right = temp;
78+
}
79+
}
80+
return root;
81+
}
82+
83+
84+
@Test
85+
public void testCase() {
86+
87+
88+
TreeNode elementLeft_2_1 = new TreeNode(10, null, null);
89+
TreeNode elementRight_2_1 = new TreeNode(60, null, null);
90+
TreeNode elementLeft_2_2_1 = new TreeNode(110, null, null);
91+
92+
TreeNode elementLeft_1_1 = new TreeNode(50, elementLeft_2_1, elementRight_2_1);
93+
TreeNode elementRight_1_1 = new TreeNode(120, elementLeft_2_2_1, null);
94+
95+
TreeNode root = new TreeNode(100, elementLeft_1_1, elementRight_1_1);
96+
97+
System.out.println("before===="+JSONObject.toJSONString(root));
98+
99+
revert(root);
100+
101+
System.out.println("after===="+JSONObject.toJSONString(root));
102+
103+
}
104+
105+
106+
}

src/main/java/com/chen/algorithm/bsearch/AdviceBsearch2.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,17 @@ public int bSearch3(int[] array, int target) {
9494
*/
9595
public int bSearch4(int[] array, int target) {
9696

97-
int left = 0, right = array.length - 1;
98-
99-
while (right >= left) {
97+
int left =0, right = array.length -1;
10098

101-
int mid = left + (right - left) / 2;
102-
103-
if (array[mid] >= target) {
104-
right = mid - 1;
105-
} else {
99+
while (left <= right){
100+
int mid = (right - left) / 2 + left;
101+
if (target > array[mid]) {
106102
left = mid + 1;
103+
} else {
104+
right = mid - 1;
107105
}
108106
}
109107

110-
111108
return left;
112109
}
113110

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.chen.algorithm.fooBarThread;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2020-08-04 17:25
9+
*/
10+
public class ConditionFooBar {
11+
12+
private ReentrantLock lock = new ReentrantLock();
13+
private Condition fooCondition = lock.newCondition();
14+
private Condition barCondition = lock.newCondition();
15+
private volatile boolean flag = true;
16+
private int n;
17+
18+
public int getN() {
19+
return n;
20+
}
21+
22+
public void setN(int n) {
23+
this.n = n;
24+
}
25+
26+
public void foo(Runnable printFoo) throws InterruptedException {
27+
for (int i = 0; i < n; i++) {
28+
lock.lock();
29+
if (flag) {
30+
fooCondition.await();
31+
}
32+
// printFoo.run() outputs "foo". Do not change or remove this line.
33+
printFoo.run();
34+
barCondition.signal();
35+
flag = true;
36+
lock.unlock();
37+
}
38+
}
39+
40+
public void bar(Runnable printBar) throws InterruptedException {
41+
for (int i = 0; i < n; i++) {
42+
lock.lock();
43+
if (!flag) {
44+
barCondition.await();
45+
}
46+
// printBar.run() outputs "bar". Do not change or remove this line.
47+
printBar.run();
48+
fooCondition.signal();
49+
flag = false;
50+
lock.unlock();
51+
}
52+
}
53+
54+
55+
56+
57+
}

0 commit comments

Comments
 (0)