Skip to content

Commit 028e140

Browse files
committed
20190301
1 parent a759659 commit 028e140

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

Diff for: code/lc237.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package code;
2+
/*
3+
* 237. Delete Node in a Linked List
4+
* 题意:删除链表中的一个节点,给的是这个节点,不知道前边的节点
5+
* 难度:Easy
6+
* 分类:Linked List
7+
* 思路:剑指Offer上有,拷贝下一个节点的内容到该节点,倒数第二个节点置空
8+
* Tips:
9+
*/
10+
public class lc237 {
11+
public class ListNode {
12+
int val;
13+
ListNode next;
14+
15+
ListNode(int x) {
16+
val = x;
17+
}
18+
}
19+
public void deleteNode(ListNode node) {
20+
ListNode pre = new ListNode(-1);
21+
while(node.next!=null) {
22+
node.val = node.next.val;
23+
pre = node;
24+
node = node.next;
25+
}
26+
pre.next = null;
27+
}
28+
}

Diff for: code/lc295.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package code;
22

33
import java.util.PriorityQueue;
4-
4+
/*
5+
* 295. Find Median from Data Stream
6+
* 题意:流数据中找中位数
7+
* 难度:Hard
8+
* 分类:
9+
* 思路:用两个优先队列,一个保存左半边最大值,一个保存右半边最小值
10+
* 保持左半边最多比右半边多一个数
11+
* Tips:
12+
*/
513
public class lc295 {
614
class MedianFinder {
715
PriorityQueue<Integer> pq1; //默认是最小,右半边

Diff for: code/lc324.java

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package code;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* 324. Wiggle Sort II
7+
* 题意:小大小大小大 这样排序
8+
* 难度:Medium
9+
* 分类:Sort
10+
* 思路:先找到中位数,然后一个小于中位数的,一个大于中位数的这样组合
11+
* 用 index map 的方式可以使空间也为O(1)
12+
* Tips:挺难的。
13+
*/
14+
public class lc324 {
15+
public void wiggleSort(int[] nums) {
16+
if(nums.length==1) return;
17+
int n = nums.length, m = (n + 1) >> 1;// (nums.length+1)/2 注意+1
18+
int[] copy = Arrays.copyOf(nums, n);
19+
int median = findMedium(nums, 0, nums.length-1, m);
20+
21+
for (int i = 0, j = 0, k = n - 1; j <= k;) { // <medium的放左边, >在右边
22+
if (copy[j] < median) {
23+
swap(copy, i++, j++);
24+
} else if (copy[j] > median) {
25+
swap(copy, j, k--);
26+
} else {
27+
j++;
28+
}
29+
}
30+
31+
for (int i = m - 1, j = 0; i >= 0; i--, j += 2) nums[j] = copy[i]; //注意这点细节,i--倒着来,防止中位数重复的情况bug
32+
for (int i = n - 1, j = 1; i >= m; i--, j += 2) nums[j] = copy[i];
33+
}
34+
35+
36+
37+
private int getMiddle(int[] nums, int l, int r) {
38+
int i = l;
39+
40+
for (int j = l + 1; j <= r; j++) {
41+
if (nums[j] < nums[l]) swap(nums, ++i, j);
42+
}
43+
44+
swap(nums, l, i);
45+
return i;
46+
}
47+
48+
private void swap(int[] nums, int i, int j) {
49+
int t = nums[i];
50+
nums[i] = nums[j];
51+
nums[j] = t;
52+
}
53+
54+
55+
public static int findMedium(int[] nums, int left, int right, int k){
56+
int cur = nums[left];
57+
int l = left;
58+
int r = right;
59+
while(left<right){
60+
while( left<right && nums[right]>=nums[left] ) right--;
61+
int temp = nums[left];
62+
nums[left] = nums[right];
63+
nums[right] = temp;
64+
while( left<right && nums[left]<nums[right]) left++;
65+
temp = nums[left];
66+
nums[left] = nums[right];
67+
nums[right] = temp;
68+
}
69+
if(left==k-1) return nums[left];
70+
else if(left>=k) return findMedium(nums, l, right-1, k);
71+
else return findMedium(nums, right+1, r, k);
72+
}
73+
74+
public static int newIndex(int index, int n) {
75+
return (1 + 2*index) % (n | 1);
76+
}
77+
}

0 commit comments

Comments
 (0)