Skip to content

Commit cbc65a3

Browse files
author
wb-hjk570755
committed
一个数组中 连续的最大和
1 parent 30436b5 commit cbc65a3

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.blankj.myself;
2+
3+
import com.blankj.myself.HasCycle_fast_slow_point.ListNode;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/3
12+
*/
13+
public class GetKthFromEnd {
14+
public static void main(String[] args) {
15+
ListNode head = new ListNode(1);
16+
ListNode tail = null;
17+
18+
//head.next=new ListNode(2);
19+
//head.next.next=new ListNode(3);
20+
//head.next.next.next=new ListNode(4);
21+
//head.next.next.next.next=new ListNode(5);
22+
23+
for (int i=1;i<5;i++){
24+
if(tail==null){
25+
tail = new ListNode(i+1);
26+
head.next = tail;
27+
}else {
28+
tail.next = new ListNode(i+1);
29+
tail = tail.next;
30+
}
31+
32+
}
33+
34+
GetKthFromEnd getKthFromEnd = new GetKthFromEnd();
35+
getKthFromEnd.getKthFromEnd(head,2);
36+
37+
38+
}
39+
40+
public ListNode getKthFromEnd(ListNode head, int k) {
41+
int size = 0;
42+
ListNode headSecond = head;
43+
while(head!=null){
44+
head = head.next;
45+
size = size + 1;
46+
}
47+
ListNode res = null;
48+
ListNode temp = null;
49+
int index = 0;
50+
while(headSecond!=null){
51+
52+
index = index + 1;
53+
if(size-k<index){
54+
if(res==null){
55+
temp = new ListNode(headSecond.val);
56+
res = temp;
57+
}else{
58+
temp.next = headSecond;
59+
temp=temp.next;
60+
}
61+
}
62+
headSecond = headSecond.next;
63+
}
64+
return res;
65+
}
66+
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/10/3
10+
*/
11+
public class MaxSubArray {
12+
13+
public int maxSubArray(int[] nums) {
14+
int res = nums[0];
15+
for(int i=1;i<nums.length;i++){
16+
nums[i] = Math.max(nums[i-1],0) + nums[i];
17+
res = Math.max(res,nums[i]);
18+
}
19+
return res;
20+
}
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.blankj.myself;
2+
3+
import com.blankj.myself.HasCycle_fast_slow_point.ListNode;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/3
12+
*/
13+
public class MergeTwoLists {
14+
15+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
16+
if(l1==null&&l2==null){
17+
return l2;
18+
}
19+
if(l1==null&&l2!=null){
20+
return l2;
21+
}
22+
if(l2==null&&l1!=null){
23+
return l1;
24+
}
25+
ListNode res ;
26+
if(l1.val<=l2.val){
27+
res = new ListNode(l1.val);
28+
res.next = mergeTwoLists(l1.next,l2);
29+
}else {
30+
res = new ListNode(l2.val);
31+
res.next = mergeTwoLists(l1,l2.next);
32+
}
33+
return res;
34+
35+
}
36+
}

src/com/blankj/myself/TreeBFS.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.blankj.myself;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.blankj.myself.InorderTraversal.TreeNode;
8+
9+
/**
10+
* Description:
11+
* Copyright: Copyright (c) 2012
12+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
13+
*
14+
* @author huangjk
15+
* @version 1.0 2020/10/3
16+
*/
17+
public class TreeBFS {
18+
public List<List<Integer>> levelOrder(TreeNode root) {
19+
List<List<Integer>> res = new ArrayList<>();
20+
if(root==null){
21+
return res;
22+
}
23+
ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
24+
queue.add(root);
25+
26+
while (queue.peek()!=null){
27+
List<Integer> temp = new ArrayList<>();
28+
int size = queue.size();
29+
for (int i=0;i<size;i++){
30+
TreeNode treeNode = queue.removeFirst();
31+
32+
if(treeNode.left != null){
33+
queue.add(treeNode.left);
34+
}
35+
if(treeNode.right != null){
36+
queue.add(treeNode.right);
37+
}
38+
39+
temp.add(treeNode.val);
40+
}
41+
42+
res.add(temp);
43+
}
44+
return res;
45+
}
46+
}

0 commit comments

Comments
 (0)