Skip to content

Commit 555dde3

Browse files
committed
update
1 parent 2478eb3 commit 555dde3

File tree

12 files changed

+516
-1
lines changed

12 files changed

+516
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fibers.algorithm.datastructure;
2+
3+
import com.fibers.utils.Utils;
4+
5+
public abstract class Heap {
6+
7+
protected int[] h;
8+
protected int N;
9+
10+
11+
public Heap(int length) {
12+
h = new int[length + 1];
13+
N = 0;
14+
}
15+
16+
abstract public void shift_up(int i);
17+
18+
abstract public void shift_down(int i);
19+
20+
abstract public void force_push(int v);
21+
22+
public void push(int v) {
23+
if (N < h.length - 1) {
24+
N++;
25+
h[N] = v;
26+
this.shift_up(N);
27+
}
28+
}
29+
30+
public int top() {
31+
if (N > 0) {
32+
return h[1];
33+
} else {
34+
return -1;
35+
}
36+
}
37+
38+
public int bottom(){
39+
if (N > 0) {
40+
return h[N];
41+
} else {
42+
return -1;
43+
}
44+
}
45+
46+
public int pop() {
47+
int v = -1;
48+
if (N > 0) {
49+
v = h[1];
50+
Utils.swap(h, 1, N);
51+
N--;
52+
this.shift_down(1);
53+
}
54+
return v;
55+
}
56+
57+
public int[] heap_sort() {
58+
int[] a = new int[N];
59+
int i = 0;
60+
while (N > 0) {
61+
a[i] = this.pop();
62+
i++;
63+
}
64+
return a;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fibers.algorithm.datastructure;
2+
3+
import com.fibers.utils.Utils;
4+
5+
public class MinHeap extends Heap {
6+
7+
public MinHeap(int length) {
8+
super(length);
9+
}
10+
11+
12+
public static void main(String[] args) {
13+
14+
MinHeap mh = new MinHeap(10);
15+
16+
int[] nums = new int[]{3, 1, 4, 6, 7, 10, 2, 5, 9, 8};
17+
for (int i : nums) {
18+
mh.push(i);
19+
}
20+
21+
Utils.printArray(mh.heap_sort());
22+
}
23+
24+
@Override
25+
public void shift_up(int i) {
26+
while (i > 1 && h[i] < h[i / 2]) {
27+
Utils.swap(h, i, i / 2);
28+
i = i / 2;
29+
}
30+
}
31+
32+
@Override
33+
public void shift_down(int i) {
34+
while (2 * i <= N) {
35+
int j = 2 * i;
36+
if (j < N && h[j] > h[j + 1]) {
37+
j += 1;
38+
}
39+
if (h[i] < h[j]) {
40+
break;
41+
}
42+
Utils.swap(h, i, j);
43+
i = j;
44+
}
45+
}
46+
47+
@Override
48+
public void force_push(int v) {
49+
if (N < h.length - 1) {
50+
this.push(v);
51+
} else {
52+
if (v > h[1]) {
53+
h[1] = v;
54+
this.shift_down(1);
55+
}
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.fibers.algorithm.leetcode._023;
2+
3+
import com.fibers.algorithm.datastructure.ListNode;
4+
import com.fibers.utils.Utils;
5+
6+
public class Solution {
7+
8+
public static void main(String[] args) {
9+
ListNode l11 = new ListNode(1);
10+
ListNode l12 = new ListNode(4);
11+
ListNode l13 = new ListNode(5);
12+
l11.next = l12;
13+
l12.next = l13;
14+
15+
ListNode l21 = new ListNode(1);
16+
ListNode l22 = new ListNode(3);
17+
ListNode l23 = new ListNode(4);
18+
l21.next = l22;
19+
l22.next = l23;
20+
21+
ListNode l31 = new ListNode(2);
22+
ListNode l32 = new ListNode(6);
23+
l31.next = l32;
24+
25+
ListNode[] lists = new ListNode[]{l11, l21, l31};
26+
27+
Solution s = new Solution();
28+
ListNode result = s.mergeKLists(lists);
29+
30+
Utils.printListNode(result);
31+
32+
}
33+
34+
public ListNode mergeKLists(ListNode[] lists) {
35+
ListNode head = new ListNode(0);
36+
ListNode p = head;
37+
38+
if (lists == null || lists.length == 0) {
39+
return head.next;
40+
}
41+
42+
while (true) {
43+
int index = compare(lists);
44+
if (index == -1) {
45+
return head.next;
46+
}
47+
ListNode node = lists[index];
48+
49+
p.next = node;
50+
lists[index] = node.next;
51+
52+
p = p.next;
53+
}
54+
}
55+
56+
private int compare(ListNode[] lists) {
57+
58+
int index = -1;
59+
int min = Integer.MAX_VALUE;
60+
61+
for (int i = 0; i < lists.length; i++) {
62+
if (lists[i] != null && lists[i].val < min) {
63+
min = lists[i].val;
64+
index = i;
65+
}
66+
}
67+
68+
return index;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fibers.algorithm.leetcode._024;
2+
3+
import com.fibers.algorithm.datastructure.ListNode;
4+
import com.fibers.utils.Utils;
5+
6+
public class Solution {
7+
8+
public static void main(String[] args) {
9+
ListNode l11 = new ListNode(1);
10+
ListNode l12 = new ListNode(2);
11+
ListNode l13 = new ListNode(3);
12+
ListNode l14 = new ListNode(4);
13+
l11.next = l12;
14+
l12.next = l13;
15+
l13.next = l14;
16+
17+
Solution s = new Solution();
18+
ListNode result = s.swapPairs(l11);
19+
20+
Utils.printListNode(result);
21+
22+
}
23+
24+
public ListNode swapPairs(ListNode head) {
25+
if (head == null || head.next == null) {
26+
return head;
27+
}
28+
29+
ListNode parent = new ListNode(-1);
30+
parent.next = head;
31+
32+
ListNode root = parent;
33+
34+
ListNode prev = head;
35+
ListNode next = head.next;
36+
37+
do {
38+
parent.next = next;
39+
40+
prev.next = next.next;
41+
next.next = prev;
42+
43+
parent = prev;
44+
} while ((prev = prev.next) != null && (next = prev.next) != null);
45+
46+
return root.next;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fibers.algorithm.leetcode._025;
2+
3+
import com.fibers.algorithm.datastructure.ListNode;
4+
import com.fibers.utils.Utils;
5+
6+
public class Solution {
7+
8+
public static void main(String[] args) {
9+
ListNode l11 = new ListNode(1);
10+
ListNode l12 = new ListNode(2);
11+
ListNode l13 = new ListNode(3);
12+
ListNode l14 = new ListNode(4);
13+
ListNode l15 = new ListNode(5);
14+
ListNode l16 = new ListNode(6);
15+
ListNode l17 = new ListNode(7);
16+
17+
l11.next = l12;
18+
l12.next = l13;
19+
l13.next = l14;
20+
l14.next = l15;
21+
l15.next = l16;
22+
l16.next = l17;
23+
24+
Solution s = new Solution();
25+
ListNode result = s.reverseKGroup(l11, 3);
26+
27+
Utils.printListNode(result);
28+
29+
}
30+
31+
32+
public ListNode reverseKGroup(ListNode head, int k) {
33+
if (head == null || k == 0) {
34+
return head;
35+
}
36+
37+
ListNode prevGroupLast = new ListNode(-1);
38+
prevGroupLast.next = head;
39+
40+
ListNode root = prevGroupLast;
41+
ListNode cur = prevGroupLast.next;
42+
ListNode curGroupFirst = null;
43+
ListNode prev = null;
44+
int i = 0;
45+
46+
while (cur != null) {
47+
ListNode next = cur.next;
48+
if (i < k) {
49+
if (i == 0) {
50+
curGroupFirst = cur;
51+
}
52+
cur.next = prev;
53+
prev = cur;
54+
cur = next;
55+
i++;
56+
} else {
57+
prevGroupLast.next = prev;
58+
prevGroupLast = curGroupFirst;
59+
i = 0;
60+
prev = null;
61+
}
62+
}
63+
64+
if( i< k){
65+
cur = prev;
66+
prev = null;
67+
68+
while (cur != null) {
69+
ListNode next = cur.next;
70+
cur.next = prev;
71+
72+
prev = cur;
73+
cur = next;
74+
}
75+
76+
prevGroupLast.next = curGroupFirst;
77+
}else{
78+
prevGroupLast.next = prev;
79+
}
80+
81+
return root.next;
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fibers.algorithm.leetcode._027;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
int nums[] = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
7+
8+
Solution s = new Solution();
9+
int len = s.removeElement(nums, 2);
10+
11+
System.out.println(len);
12+
}
13+
14+
public int removeElement(int[] nums, int val) {
15+
if (nums == null || nums.length == 0) {
16+
return 0;
17+
}
18+
19+
int first = 0;
20+
int last = nums.length - 1;
21+
22+
while (first <= last) {
23+
24+
if (nums[first] == val && nums[last] != val) {
25+
int temp = nums[first];
26+
nums[first] = nums[last];
27+
nums[last] = temp;
28+
} else if (nums[first] != val) {
29+
first++;
30+
} else {
31+
last--;
32+
}
33+
}
34+
35+
return first;
36+
}
37+
}

0 commit comments

Comments
 (0)