File tree Expand file tree Collapse file tree 4 files changed +170
-0
lines changed Expand file tree Collapse file tree 4 files changed +170
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments