Skip to content

Commit 466967f

Browse files
author
chenweijie
committed
add test
1 parent 36bb081 commit 466967f

File tree

16 files changed

+517
-7
lines changed

16 files changed

+517
-7
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.chen.algorithm.study.test121;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* 双指针算法
7+
*
8+
* @author : chen weijie
9+
* @Date: 2019-11-01 23:54
10+
*/
11+
public class Solution3 {
12+
13+
14+
public int maxProfit(int[] prices) {
15+
16+
if (prices == null || prices.length == 0) {
17+
return 0;
18+
}
19+
20+
int min = Integer.MAX_VALUE;
21+
int max = 0;
22+
for (int price : prices) {
23+
min = Math.min(price, min);
24+
max = Math.max(max, price - min);
25+
}
26+
return max;
27+
}
28+
29+
30+
@Test
31+
public void testCase() {
32+
33+
int[] n = {7, 6, 4, 3, 1};
34+
35+
System.out.println(maxProfit(n));
36+
}
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.chen.algorithm.study.test122;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-05-16 23:36
6+
*/
7+
public class Solution2 {
8+
9+
10+
public static int maxProfit(int[] prices) {
11+
12+
if (prices == null || prices.length == 0) {
13+
return 0;
14+
}
15+
16+
17+
int sum = 0;
18+
for (int i = 1; i < prices.length; i++) {
19+
if (prices[i - 1] < prices[i]) {
20+
sum = sum + (prices[i] - prices[i - 1]);
21+
}
22+
}
23+
return sum;
24+
}
25+
26+
27+
public static void main(String[] args) {
28+
int[] n = {7, 1, 5, 3, 6, 4};
29+
System.out.println(maxProfit(n));
30+
31+
}
32+
33+
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.chen.algorithm.study.test15;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* @author : chen weijie
9+
* @Date: 2020-05-22 01:50
10+
*/
11+
public class Solution2 {
12+
13+
public List<List<Integer>> threeSum(int[] nums) {
14+
15+
List<List<Integer>> res = new ArrayList<>();
16+
17+
if (nums == null || nums.length == 0) {
18+
return res;
19+
}
20+
21+
Arrays.sort(nums);
22+
for (int i = 0; i < nums.length; i++) {
23+
if (nums[i] > 0) {
24+
break;
25+
}
26+
if (i > 0 && nums[i] == nums[i - 1]) {
27+
continue;
28+
}
29+
30+
int L = i + 1, R = nums.length - 1;
31+
while (L < R) {
32+
int sum = nums[i] + nums[L] + nums[R];
33+
if (sum == 0) {
34+
res.add(Arrays.asList(nums[i], nums[L], nums[R]));
35+
while (L < R && nums[L] == nums[L + 1]) {
36+
L++;
37+
}
38+
while (L < R && nums[R] == nums[R - 1]) {
39+
R--;
40+
}
41+
L++;
42+
R--;
43+
} else if (sum > 0) {
44+
R--;
45+
} else {
46+
L++;
47+
}
48+
}
49+
50+
}
51+
return res;
52+
}
53+
54+
55+
}

src/main/java/com/chen/algorithm/study/test160/Solution1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
3939
pB = headA;
4040
}
4141
}
42-
return null;
42+
return pB;
4343
}
4444

4545

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.algorithm.study.test2;
22

3+
import org.junit.Test;
4+
35
/**
46
* @author : chen weijie
57
* @Date: 2019-09-07 16:16
@@ -33,8 +35,34 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3335
}
3436
return dummmy.next;
3537
}
38+
@Test
39+
public void testCase() {
40+
41+
ListNode l1_1 = new ListNode(2);
42+
ListNode l1_2 = new ListNode(4);
43+
ListNode l1_3 = new ListNode(3);
44+
45+
l1_1.next = l1_2;
46+
l1_2.next = l1_3;
47+
48+
49+
ListNode l2_1 = new ListNode(5);
50+
ListNode l2_2 = new ListNode(6);
51+
ListNode l2_3 = new ListNode(4);
52+
53+
l2_1.next = l2_2;
54+
l2_2.next = l2_3;
3655

3756

57+
ListNode result = addTwoNumbers(l1_1, l2_1);
58+
59+
System.out.println(result.val);
60+
System.out.println(result.next.val);
61+
System.out.println(result.next.next.val);
62+
63+
64+
}
65+
3866

3967

4068
}

src/main/java/com/chen/algorithm/study/test206/Solution3.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ public ListNode reverseList(ListNode head) {
1515
return head;
1616
}
1717

18-
ListNode pre = null;
18+
ListNode dummy = new ListNode(-1);
19+
dummy.next = head;
1920
while (head != null) {
2021
ListNode temp = head.next;
21-
head.next = pre;
22-
pre = head;
22+
head.next = dummy;
23+
dummy = head;
2324
head = temp;
2425
}
25-
return pre;
26+
return dummy;
2627
}
2728

2829

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.chen.algorithm.study.test24;
2+
3+
/**
4+
* 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
5+
* <p>
6+
* 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换
7+
*
8+
* @author : chen weijie
9+
* @Date: 2020-05-03 11:27
10+
*/
11+
public class Solution2 {
12+
13+
14+
public ListNode swapPairs(ListNode head) {
15+
16+
if (head == null) {
17+
return null;
18+
}
19+
20+
ListNode dummy = new ListNode(-1);
21+
dummy.next = head;
22+
23+
ListNode prev = dummy;
24+
25+
while (head != null && head.next != null) {
26+
27+
ListNode firstNode = head;
28+
ListNode secondNode = head.next;
29+
30+
31+
firstNode.next = secondNode.next;
32+
secondNode.next = firstNode;
33+
prev.next = secondNode;
34+
35+
prev = firstNode;
36+
head = firstNode.next;
37+
}
38+
39+
return dummy.next;
40+
}
41+
42+
43+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public int lengthOfLongestSubstring(String s) {
4646

4747
@Test
4848
public void testCase() {
49-
System.out.println(lengthOfLongestSubstring("cdfg"));
49+
System.out.println(lengthOfLongestSubstring("cdfcg"));
5050
}
5151

5252

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.chen.algorithm.study.test300;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/zui-chang-shang-sheng-zi-xu-lie-dong-tai-gui-hua-2/
7+
*
8+
* @author : chen weijie
9+
* @Date: 2019-12-22 16:28
10+
*/
11+
public class Solution2 {
12+
13+
public int lengthOfList(int[] nums) {
14+
15+
if (nums == null || nums.length == 0) {
16+
return 0;
17+
}
18+
int[] dp = new int[nums.length];
19+
dp[0] = 1;
20+
int max = 1;
21+
for (int i = 1; i < dp.length; i++) {
22+
int tem = 0;
23+
for (int j = 0; j < i; j++) {
24+
if (nums[j] < nums[i]) {
25+
tem = Math.max(tem, dp[j]);
26+
}
27+
}
28+
dp[i] = tem + 1;
29+
max = Math.max(dp[i], max);
30+
}
31+
return max;
32+
}
33+
34+
35+
@Test
36+
public void testCase() {
37+
int[] n = {4, 1, -4, 7, -2, 9, 0};
38+
System.out.println(lengthOfList(n));
39+
}
40+
41+
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.chen.algorithm.study.test322;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* https://leetcode-cn.com/problems/coin-change/solution/322-ling-qian-dui-huan-by-leetcode-solution/
9+
*
10+
* @author : chen weijie
11+
* @Date: 2020-05-24 00:43
12+
*/
13+
public class Solution {
14+
15+
public int coinChange(int[] coins, int amount) {
16+
int max = amount + 1;
17+
int[] dp = new int[amount + 1];
18+
Arrays.fill(dp, max);
19+
dp[0] = 0;
20+
for (int i = 0; i < dp.length; i++) {
21+
for (int coin : coins) {
22+
if (coin <= i) {
23+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
24+
}
25+
}
26+
}
27+
return dp[amount] > amount ? -1 : dp[amount];
28+
}
29+
30+
31+
@Test
32+
public void testCase() {
33+
int[] coins = {2};
34+
System.out.println(coinChange(coins, 3));
35+
}
36+
37+
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.chen.algorithm.study.test50;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-05-22 02:52
6+
*/
7+
public class Solution3 {
8+
9+
10+
public double myPow(double x, int n) {
11+
12+
13+
if (n < 0) {
14+
x = 1 / x;
15+
n = -n;
16+
}
17+
return quick(x, n);
18+
}
19+
20+
21+
public double quick(double x, int n) {
22+
23+
if (n == 0) {
24+
return 1d;
25+
}
26+
double y = quick(x, n / 2);
27+
return n % 2 == 0 ? y * y : y * y * x;
28+
}
29+
30+
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chen.algorithm.study.test674;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2020-05-24 00:15
6+
*/
7+
public class Solution {
8+
9+
public int findLengthOfLCIS(int[] nums) {
10+
11+
if (nums == null || nums.length == 0) {
12+
return 0;
13+
}
14+
15+
int[] dp = new int[nums.length];
16+
dp[0] = 1;
17+
int max = 1;
18+
for (int i = 1; i < nums.length; i++) {
19+
if (nums[i] > nums[i - 1]) {
20+
dp[i] = dp[i - 1] + 1;
21+
} else {
22+
dp[i] = 1;
23+
}
24+
max = Math.max(max, dp[i]);
25+
}
26+
return max;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)