Skip to content

Commit 96fedcb

Browse files
author
caikun1
committed
更新
1 parent 1859567 commit 96fedcb

File tree

5 files changed

+309
-25
lines changed

5 files changed

+309
-25
lines changed

src/main/java/ind/ck/construct/LinkedList.java

+53-22
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@
88
public class LinkedList {
99

1010

11-
private Node head = new Node();
11+
private ListNode head = new ListNode();
1212

1313
public void add(int value) {
14-
Node node = head;
15-
while (node.next != null) {
16-
node = node.next;
14+
ListNode listNode = head;
15+
while (listNode.next != null) {
16+
listNode = listNode.next;
1717
}
18-
node.next = new Node(value, null);
18+
listNode.next = new ListNode(value, null);
1919
}
2020

2121
public void show() {
22-
Node node = head;
23-
while (node != null) {
24-
System.out.println(node.value);
25-
node = node.next;
22+
ListNode listNode = head;
23+
while (listNode != null) {
24+
System.out.println(listNode.value);
25+
listNode = listNode.next;
2626
}
2727
}
2828

29-
public Node getHead() {
29+
public ListNode getHead() {
3030
return this.head;
3131
}
3232

33-
public void revert(Node nodeNotHead) {
34-
if (nodeNotHead == null | nodeNotHead.next == null) {
33+
public void revert(ListNode listNodeNotHead) {
34+
if (listNodeNotHead == null | listNodeNotHead.next == null) {
3535
return;
3636
}
37-
Node nxt = nodeNotHead.next;
38-
Node nxtnxt = nxt.next;
39-
Node headNxt = head.next;
37+
ListNode nxt = listNodeNotHead.next;
38+
ListNode nxtnxt = nxt.next;
39+
ListNode headNxt = head.next;
4040
// 头节点的下一个去链接当前节点的下一个
4141
head.next = nxt;
4242
// 现在当前节点的下一个和头节点的下一个是相同的
@@ -45,23 +45,49 @@ public void revert(Node nodeNotHead) {
4545
nxt.next = headNxt;
4646
// 现在当前节点的下一个还是那个老的节点,
4747
// 当前的下一个变为当前的下下个
48-
nodeNotHead.next = nxtnxt;
49-
revert(nodeNotHead);
48+
listNodeNotHead.next = nxtnxt;
49+
revert(listNodeNotHead);
50+
}
51+
52+
public ListNode reverseList(ListNode head) {
53+
// 各种方式判空和单节点
54+
if (head == null || head.next == null) {
55+
return head;
56+
}
57+
// 依次得到第{2,3...n}个节点b
58+
ListNode b = head.next;
59+
// 依次得到第{1... n-1}个节点a
60+
ListNode a = head;
61+
// 重要:将头节点的下一个关系干掉
62+
head.next = null;
63+
// 当b不为空时
64+
while (b != null) {
65+
// 依次修改向前指向
66+
67+
// b的下一个为a
68+
ListNode c = b.next;
69+
b.next = a;
70+
// ab颠倒
71+
a = b;
72+
b = c;
73+
}
74+
// 返回head-next
75+
return head;
5076
}
5177

5278

53-
public class Node {
79+
public class ListNode {
5480

55-
Node next;
81+
ListNode next;
5682

5783
int value;
5884

59-
public Node(int value, Node next) {
85+
public ListNode(int value, ListNode next) {
6086
this.value = value;
6187
this.next = next;
6288
}
6389

64-
private Node() {
90+
private ListNode() {
6591
// 头节点
6692
this.value = -1;
6793
this.next = null;
@@ -76,8 +102,13 @@ public static void main(String[] args) {
76102
ll.add(3);
77103
ll.add(4);
78104
ll.add(5);
105+
ll.add(6);
106+
ll.add(7);
107+
ll.add(8);
108+
ll.add(9);
109+
ll.add(10);
79110
ll.show();
80-
ll.revert(ll.getHead().next);
111+
ll.reverseList(ll.getHead().next);
81112
ll.show();
82113

83114
}

src/main/java/ind/ck/dp/EzDp.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,36 @@ public static int minCashNumDp(int w) {
224224
}
225225

226226

227+
/**
228+
* 最大子序和
229+
* https://leetcode-cn.com/problems/maximum-subarray/solution/
230+
* @param nums
231+
* @return
232+
*/
233+
public static int maxSubArray(int[] nums) {
234+
// nil judge
235+
if (nums == null || nums.length == 0) {
236+
return 0;
237+
}
238+
// 初始化 最大子序合为 res为第一个元素,
239+
int res = nums[0];
240+
// 初始化,阶段最大tmpRes为0;
241+
int tmpRes = 0;
242+
// 我们想下这个题目的细节,实际上找的是:
243+
// 第一个正数、从前向后累加、后面出现的负数不会把前面正值给清零或者变负的 到符合这个条件最后一个正数的位置
244+
for (int i = 0; i < nums.length; i ++) {
245+
if (tmpRes > 0) {
246+
// 当前tmpRes 还大于0,说明还能救
247+
tmpRes += nums[i];
248+
} else {
249+
// 已经小于=0了,tmpRes 看起来可以放弃了,需要从头计数了,重新赋值吧
250+
tmpRes = nums[i];
251+
}
252+
res = Math.max(res, tmpRes);
253+
}
254+
return res;
255+
}
256+
227257
public static void main(String[] args) {
228258
// System.out.println(cutting(10));
229259
// System.out.println(isAliasWon(10, true));
@@ -233,7 +263,8 @@ public static void main(String[] args) {
233263
// 1;1;1 2;1 1;2
234264
// System.out.println(jumpMethod(3));
235265

236-
System.out.println(minCashNumDp(100));
266+
// System.out.println(minCashNumDp(100));
267+
System.out.println(maxSubArray(new int[]{-2,1,-3,4,-1,2,1,-5,4}) );
237268
}
238269

239270

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package ind.ck.leetcode;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* @Author caikun
8+
* @Description //TODO $END
9+
* @Date 上午10:34 21-4-1
10+
**/
11+
public class ArrayTest {
12+
13+
public static int reverse(int x) {
14+
// 设置正负标志后得绝对值ab
15+
// boolean neg = x < 0;
16+
int ab = x;
17+
int res = 0;
18+
// 当ab != 0 时
19+
while (ab != 0) {
20+
// 得到最后一位 c = ab % 10
21+
int c = ab % 10;
22+
ab /= 10;
23+
// 不为0 则 * 10 + c | 为0这一顿操作也没意义
24+
int resTmp = res;
25+
res = res * 10 + c;
26+
// 判断溢出
27+
if ((res - c)/10 != resTmp) {
28+
return 0;
29+
}
30+
}
31+
return res;
32+
33+
}
34+
35+
public static String longestCommonPrefix(String[] strs) {
36+
// pankong
37+
if (strs == null || strs.length == 0) {
38+
return "";
39+
}
40+
// 设定fst为数组第一个元素,和最长位置i
41+
int i = 0;
42+
String fst = strs[0];
43+
for (i = 0; i < fst.length() ; i ++) {
44+
// 获取本列
45+
String ai = fst.substring(i, i + 1);
46+
if (!right(strs, ai, i)) {
47+
break;
48+
}
49+
}
50+
// 输出fts
51+
return fst.substring(0, i);
52+
}
53+
54+
private static boolean right(String[] strs, String ai, int i) {
55+
for (int j = 1;j < strs.length; j ++) {
56+
// 循环strs
57+
// 获取每个的第i个符号,
58+
// 超长或者不等 就直接返回
59+
if (strs[j].length() <= i ) {
60+
return false;
61+
}
62+
if (!strs[j].substring(i, i + 1).equals(ai)) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
69+
/**
70+
* https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/
71+
* 无重复字符的最长子序列
72+
* @param s "abcabcbb"
73+
* @return
74+
*/
75+
public static int lengthOfLongestSubstring(String s) {
76+
// nul judge
77+
if (s == null || s.length() == 0) {
78+
return 0;
79+
}
80+
// left right hashSet init
81+
int left = 0;
82+
int right = 0;
83+
int sum = 0;
84+
Set<Character> hset = new HashSet<Character>();
85+
// fori
86+
for (;right < s.length();) {
87+
Character tmp = s.charAt(right);
88+
// haseSet contains
89+
if (hset.contains(tmp)) {
90+
// hset clear, left ++,right = left
91+
left ++;
92+
right = left;
93+
hset.clear();
94+
} else {
95+
// not contain
96+
right ++;
97+
// ====right ++
98+
hset.add(tmp);
99+
// hset.add
100+
sum = Math.max(sum, right - left);
101+
// sum = max(right - left, sum)
102+
}
103+
}
104+
return sum;
105+
}
106+
107+
108+
public static void main(String[] args) {
109+
System.out.println(lengthOfLongestSubstring("abcabcbb"));
110+
// System.out.println(longestCommonPrefix(new String[]{"cog","dacecar","dar"}));
111+
112+
// String ab = "abc";
113+
// System.out.println();
114+
// System.out.println(ab.substring(0, 0));
115+
// int i = 0;
116+
// for (i =0; i< 1; i++) {
117+
// break;
118+
// }
119+
// System.out.println("dd" + i);
120+
//
121+
// System.out.println(reverse(123));
122+
}
123+
}

0 commit comments

Comments
 (0)