Skip to content

Commit a34cf58

Browse files
author
chenweijie
committed
算法
1 parent 2e22d76 commit a34cf58

39 files changed

+1295
-11
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<mongodb.driver.version>3.2.2</mongodb.driver.version>
1212
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
1313
<spring.version>4.3.11.RELEASE</spring.version>
14+
<lombok.version>1.16.10</lombok.version>
1415
</properties>
1516

1617
<dependencies>
@@ -86,6 +87,19 @@
8687
<version>${mysql-connector-java.version}</version>
8788
</dependency>
8889

90+
<dependency>
91+
<groupId>org.apache.kafka</groupId>
92+
<artifactId>kafka_2.11</artifactId>
93+
<version>0.10.0.1</version>
94+
</dependency>
95+
96+
97+
<dependency>
98+
<groupId>com.alibaba.csp</groupId>
99+
<artifactId>sentinel-core</artifactId>
100+
<version>1.6.3</version>
101+
</dependency>
102+
89103

90104
<!-- easypoi -->
91105
<dependency>
@@ -117,6 +131,12 @@
117131
<version>0.10</version>
118132
</dependency>
119133

134+
<dependency>
135+
<groupId>org.projectlombok</groupId>
136+
<artifactId>lombok</artifactId>
137+
<version>${lombok.version}</version>
138+
</dependency>
139+
120140
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
121141
<dependency>
122142
<groupId>org.apache.zookeeper</groupId>

src/main/java/com/chen/algorithm/study/test1/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
/**
6+
* 正确
67
* @author : chen weijie
78
* @Date: 2019-09-02 23:52
89
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.chen.algorithm.study.test1;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2019-09-07 00:34
9+
*/
10+
public class Solution2 {
11+
12+
13+
public int[] twoSum(int[] nums, int target) {
14+
if (nums == null || nums.length == 0) {
15+
return new int[]{-1, -1};
16+
}
17+
18+
int[] res = {-1, -1};
19+
Map<Integer, Integer> map = new HashMap<>(nums.length);
20+
21+
for (int i = 0; i < nums.length; i++) {
22+
if (map.containsKey(target - nums[i])) {
23+
res[0] = i;
24+
res[1] = map.get(target - nums[i]);
25+
break;
26+
} else {
27+
map.put(nums[i], i);
28+
}
29+
}
30+
return res;
31+
}
32+
33+
34+
35+
36+
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.chen.algorithm.study.test10;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-09-08 02:26
6+
*/
7+
public class Solution {
8+
9+
10+
11+
}

src/main/java/com/chen/algorithm/study/test13/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
/**
6+
* 正确
67
* @author : chen weijie
78
* @Date: 2019-09-04 23:18
89
*/

src/main/java/com/chen/algorithm/study/test14/Solution2.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,15 @@ public String longestCommonPrefix(String[] strs) {
2929

3030

3131
@Test
32-
public void testCase(){
32+
public void testCase() {
3333

3434

35-
String [] strings = {"flower","flow","flight"};
35+
String[] strings = {"flower", "flow", "flight"};
3636

3737
System.out.println(longestCommonPrefix(strings));
3838

3939

40-
4140
}
4241

4342

44-
45-
46-
4743
}

src/main/java/com/chen/algorithm/study/test2/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Test;
44

55
/**
6+
* 需要思考下,写了好久才写出来
67
* @author : chen weijie
78
* @Date: 2019-09-02 23:05
89
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.chen.algorithm.study.test2;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-09-07 16:16
6+
*/
7+
public class Solution2 {
8+
9+
10+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11+
ListNode dummmy = new ListNode(-1);
12+
ListNode curr = dummmy;
13+
14+
int sum = 0;
15+
ListNode p1 = l1,p2 = l2;
16+
while (p1 != null || p2 != null) {
17+
if (p1 != null) {
18+
sum += p1.val;
19+
p1 = p1.next;
20+
}
21+
if (p2 != null) {
22+
sum += p2.val;
23+
p2 = p2.next;
24+
}
25+
26+
curr.next = new ListNode(sum % 10);
27+
curr = curr.next;
28+
sum /= 10;
29+
}
30+
31+
if (sum == 1) {
32+
curr.next = new ListNode(sum);
33+
}
34+
return dummmy.next;
35+
}
36+
37+
38+
39+
40+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.chen.algorithm.study.test20;
2+
3+
import org.junit.Test;
4+
5+
import java.math.BigDecimal;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
10+
11+
/**
12+
* 错误
13+
*
14+
* @author : chen weijie
15+
* @Date: 2019-09-05 23:10
16+
*/
17+
public class Solution {
18+
19+
20+
public boolean isValid(String s) {
21+
22+
if (s == null) {
23+
return false;
24+
}
25+
26+
if ("".equals(s)) {
27+
return true;
28+
}
29+
30+
Map<String, Double> map = new HashMap<>(6);
31+
32+
map.put("(", 0.3);
33+
map.put(")", -0.3);
34+
map.put("[", 3.0);
35+
map.put("]", -3.0);
36+
map.put("{", 30.0);
37+
map.put("}", -30.0);
38+
39+
char[] chars = s.toCharArray();
40+
41+
if (chars.length == 1) {
42+
return false;
43+
}
44+
45+
if (chars.length % 2 != 0) {
46+
return false;
47+
}
48+
49+
double count = 0;
50+
51+
for (char aChar : chars) {
52+
count += map.get(String.valueOf(aChar));
53+
}
54+
55+
if (count != 0) {
56+
return false;
57+
}
58+
59+
int index = 0;
60+
for (int i = 0; i < chars.length - 1; i++) {
61+
if (map.get(String.valueOf(chars[i])) + map.get(String.valueOf(chars[i + 1])) == 0 && ((i + 1) == chars.length / 2)) {
62+
index = i;
63+
}
64+
}
65+
66+
67+
double left = 0, right = 0;
68+
int a = index, b = index;
69+
70+
for (int i = index; i < chars.length && a > 0 && b < chars.length; i++) {
71+
left += map.get(String.valueOf(chars[a]));
72+
right += map.get(String.valueOf(chars[b]));
73+
a = i--;
74+
b = i++;
75+
}
76+
77+
return new BigDecimal(left).compareTo(new BigDecimal(right))==0;
78+
}
79+
80+
81+
@Test
82+
public void testCase() {
83+
84+
System.out.println(isValid("(]"));
85+
86+
}
87+
88+
89+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.chen.algorithm.study.test20;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Stack;
8+
9+
/**
10+
* 初始化栈 S。
11+
* 一次处理表达式的每个括号。
12+
* 如果遇到开括号,我们只需将其推到栈上即可。这意味着我们将稍后处理它,让我们简单地转到前面的 子表达式。
13+
* 如果我们遇到一个闭括号,那么我们检查栈顶的元素。如果栈顶的元素是一个 相同类型的 左括号,那么我们将它从栈中弹出并继续处理。否则,这意味着表达式无效。
14+
* 如果到最后我们剩下的栈中仍然有元素,那么这意味着表达式无效。
15+
*
16+
* @author : chen weijie
17+
* @Date: 2019-09-06 00:10
18+
*/
19+
public class Solution2 {
20+
21+
22+
public boolean isValid(String s) {
23+
24+
25+
Map<Character, Character> map = new HashMap<>(3);
26+
map.put('{', '}');
27+
map.put('[', ']');
28+
map.put('(', ')');
29+
30+
Stack<Character> stack = new Stack<>();
31+
32+
char[] chars = s.toCharArray();
33+
for (char aChar : chars) {
34+
if (map.containsKey(aChar)) {
35+
stack.push(aChar);
36+
} else {
37+
if (stack.empty()) {
38+
return false;
39+
}
40+
Character c = stack.pop();
41+
if (aChar != (map.get(c))) {
42+
return false;
43+
}
44+
}
45+
}
46+
return stack.empty();
47+
}
48+
49+
50+
@Test
51+
public void testCase() {
52+
53+
System.out.println(isValid("[](){}"));
54+
55+
56+
}
57+
58+
59+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.chen.algorithm.study.test206;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-09-06 02:00
6+
*/
7+
public class ListNode {
8+
9+
int val;
10+
ListNode next;
11+
12+
ListNode(int x) {
13+
val = x;
14+
}
15+
16+
17+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.chen.algorithm.study.test206;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Stack;
6+
7+
/**
8+
* 空间和时间复杂度都不满足要求
9+
*
10+
* @author : chen weijie
11+
* @Date: 2019-09-06 02:00
12+
*/
13+
public class Solution {
14+
15+
16+
public ListNode reverseList(ListNode head) {
17+
Stack<ListNode> stack = new Stack<>();
18+
19+
while (head != null) {
20+
stack.push(head);
21+
head = head.next;
22+
}
23+
24+
25+
ListNode node = new ListNode(0);
26+
ListNode temp = node;
27+
while (!stack.empty()) {
28+
temp.next = stack.pop();
29+
temp = temp.next;
30+
}
31+
return node.next;
32+
}
33+
34+
35+
@Test
36+
public void testCase() {
37+
38+
ListNode l1_1 = new ListNode(3);
39+
ListNode l1_2 = new ListNode(6);
40+
ListNode l1_3 = new ListNode(9);
41+
l1_1.next = l1_2;
42+
l1_2.next = l1_3;
43+
44+
ListNode result = reverseList(l1_1);
45+
46+
System.out.println(result.val);
47+
System.out.println(result.next.val);
48+
System.out.println(result.next.next.val);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)