Skip to content

Commit 2fc6fdc

Browse files
author
chenweijie
committed
update
1 parent b2a1c09 commit 2fc6fdc

File tree

8 files changed

+212
-24
lines changed

8 files changed

+212
-24
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.chen.algorithm.study.test19;
2+
3+
/**
4+
* 正确,写的不规整
5+
*
6+
* @author : chen weijie
7+
* @Date: 2019-11-10 00:25
8+
*/
9+
public class Solution3 {
10+
11+
12+
class ListNode {
13+
int val;
14+
ListNode next;
15+
16+
ListNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
public ListNode removeNthFromEnd(ListNode head, int n) {
22+
23+
if (head == null){
24+
return head;
25+
}
26+
27+
ListNode pre = new ListNode(-1);
28+
pre.next = head;
29+
30+
ListNode fast = pre;
31+
ListNode slow = pre;
32+
33+
for (int i = 0; i < n-1; i++) {
34+
fast = fast.next;
35+
}
36+
37+
while (fast.next != null) {
38+
fast = fast.next;
39+
slow = slow.next;
40+
}
41+
slow.next = slow.next.next;
42+
return pre.next;
43+
}
44+
45+
46+
}

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.chen.algorithm.study.test206;
22

3-
import org.junit.Test;
4-
53
/**
64
* @author : chen weijie
75
* @Date: 2019-09-08 01:38
86
*/
97
public class SolutionTest {
108

119

12-
public ListNode reverseList(ListNode head) {
10+
public static ListNode reverseList(ListNode head) {
1311

1412
if (head == null || head.next == null) {
1513
return head;
@@ -25,9 +23,8 @@ public ListNode reverseList(ListNode head) {
2523
return pre;
2624
}
2725

28-
@Test
29-
public void testCase() {
3026

27+
public static void main(String[] args) {
3128
ListNode l1_1 = new ListNode(4);
3229
ListNode l1_2 = new ListNode(6);
3330
ListNode l1_3 = new ListNode(10);
@@ -42,3 +39,13 @@ public void testCase() {
4239
}
4340

4441
}
42+
43+
class ListNode {
44+
45+
int val;
46+
ListNode next;
47+
48+
ListNode(int x) {
49+
val = x;
50+
}
51+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public ListNode reverseKGroup(ListNode head, int k) {
2929
ListNode tail = dummy;
3030

3131
while (true) {
32-
3332
int count = 0;
3433
while (tail != null && count != k) {
3534
tail = tail.next;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ListNode reverseKGroup(ListNode head, int k) {
2727
while (true) {
2828

2929
int count = 0;
30-
while (tail != null || count != k) {
30+
while (tail != null && count != k) {
3131
count++;
3232
tail = tail.next;
3333
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.chen.algorithm.study.test25;
2+
3+
/**
4+
* https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/
5+
*
6+
* @author : chen weijie
7+
* @Date: 2020-09-20 01:29
8+
*/
9+
public class Solution4 {
10+
11+
class ListNode {
12+
int val;
13+
ListNode next;
14+
15+
ListNode(int x) {
16+
val = x;
17+
}
18+
}
19+
20+
public ListNode reverseKGroup(ListNode head, int k) {
21+
22+
if (head == null) {
23+
return head;
24+
}
25+
26+
ListNode dummy = new ListNode(-1);
27+
dummy.next = head;
28+
ListNode pre = dummy;
29+
ListNode tail = dummy;
30+
31+
32+
while (true) {
33+
int count = 0;
34+
while (tail != null && count != k) {
35+
tail = tail.next;
36+
count++;
37+
}
38+
39+
if (tail == null) {
40+
break;
41+
}
42+
43+
ListNode head1 = pre.next;
44+
45+
while (pre.next != tail) {
46+
ListNode curr = pre.next;
47+
pre.next = curr.next;
48+
curr.next = tail.next;
49+
tail.next = curr;
50+
}
51+
52+
tail = head1;
53+
pre = head1;
54+
}
55+
56+
return dummy.next;
57+
}
58+
59+
60+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.chen.algorithm.study.test3;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 写的不简洁
10+
*
11+
* @author : chen weijie
12+
* @Date: 2019-09-03 00:00
13+
*/
14+
public class Solution4 {
15+
16+
public int lengthOfLongestSubstring(String s) {
17+
18+
if (s == null || "".equals(s)) {
19+
return 0;
20+
}
21+
Map<Character, Integer> map = new HashMap<>();
22+
23+
int max = 0;
24+
int left = 0;
25+
26+
for (int i = 0; i < s.length(); i++) {
27+
char c = s.charAt(i);
28+
29+
if (map.containsKey(c)) {
30+
left = Math.max(left, map.get(c));
31+
}
32+
max = Math.max(max, i - left);
33+
map.put(c, i);
34+
}
35+
return max;
36+
}
37+
38+
39+
@Test
40+
public void testCase() {
41+
System.out.println(lengthOfLongestSubstring("cdfcg"));
42+
}
43+
44+
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.chen.algorithm.study.test300;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/zui-chang-shang-sheng-zi-xu-lie-dong-tai-gui-hua-2/
9+
*
10+
* @author : chen weijie
11+
* @Date: 2019-12-22 16:28
12+
*/
13+
public class Solution3 {
14+
15+
public int lengthOfList(int[] nums) {
16+
17+
if (nums == null || nums.length == 0) {
18+
return 0;
19+
}
20+
int len = nums.length;
21+
22+
int[] dp = new int[len];
23+
int max = 1;
24+
Arrays.fill(dp, 1);
25+
for (int i = 1; i < len; i++) {
26+
for (int j = 0; j < i; j++) {
27+
if (nums[i] > nums[j]) {
28+
dp[i] = Math.max(dp[i], dp[j] + 1);
29+
}
30+
}
31+
max = Math.max(dp[i], max);
32+
}
33+
34+
return max;
35+
}
36+
37+
38+
@Test
39+
public void testCase() {
40+
int[] n = {4, 1, -4, 7, -2, 9, 0};
41+
42+
System.out.println(lengthOfList(n));
43+
44+
45+
}
46+
47+
48+
}

0 commit comments

Comments
 (0)