Skip to content

Commit 5629e93

Browse files
committed
fd
1 parent b73844e commit 5629e93

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

extras.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 其它题目
22

33
### Google
4-
- [Remove consecutive node from single list](leetcode/solution/src/RemoveDuplicateList.java)
4+
- [Remove consecutive node from single list](leetcode/solution/src/RemoveDuplicateList.java)
5+
- [Reverse pairs](leetcode/solution/src/CountReversePairs.java)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class CountReversePairs {
2+
3+
/**
4+
* 数组arr,[0,m)和[m,arr.length)两个区间都是递增的
5+
* 如果[0,m)区间的数比[m, arr.len)区间大,则能构成一个reverse pair
6+
* 问有多少个reverse pair
7+
*/
8+
int reversePairs(int[] arr, int m) {
9+
int pairs = 0;
10+
for (int i = m - 1, j = arr.length - 1; j >= m; j--) {
11+
for ( ; i >= 0 && arr[i] > arr[j]; i--);
12+
pairs += m - 1 - i;
13+
}
14+
return pairs;
15+
}
16+
}
17+

leetcode/solution/src/RemoveDuplicateList.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,51 @@ public class RemoveDuplicateList {
77
public ListNode removeDup(ListNode head) {
88
ListNode dummy = new ListNode(0), cur = dummy;
99

10-
ListNode node = null;
10+
ListNode last = null;
1111
int count = 0;
1212

1313
for (ListNode p = head; p != null; p = p.next) {
14-
if (node == null || node.val == p.val) {
15-
node = p;
14+
if (last == null || last.val == p.val) {
15+
last = p;
1616
count++;
1717
} else {
1818
if (count == 1) {
19-
cur.next = node;
19+
cur.next = last;
2020
cur = cur.next;
2121
}
22-
node = p;
22+
last = p;
2323
count = 1;
2424
}
2525
}
2626

27-
if (node != null && count == 1) {
28-
cur.next = node;
27+
if (last != null && count == 1) {
28+
cur.next = last;
2929
cur = cur.next;
3030
}
3131

3232
cur.next = null;
3333
return dummy.next;
3434
}
35+
36+
// 可在上面的基础上精简
37+
// count是可以去掉的
38+
public ListNode removeDup2(ListNode head) {
39+
if (head == null) {
40+
return null;
41+
}
42+
ListNode dummy = new ListNode(0), cur = dummy;
43+
ListNode last = head;
44+
45+
for (ListNode p = head.next; p != null; p = p.next) {
46+
if (last.val != p.val) {
47+
if (last.next == p) {
48+
cur.next = last;
49+
cur = cur.next;
50+
}
51+
last = p;
52+
}
53+
}
54+
cur.next = (last.next == null ? last : null);
55+
return dummy.next;
56+
}
3557
}

leetcode/src/Main.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,23 @@ public class Main {
33

44
public static class Solution {
55

6-
ListNode removeDup(ListNode head) {
7-
ListNode dummy = new ListNode(0), cur = dummy;
8-
9-
ListNode node = null;
10-
int count = 0;
11-
12-
for (ListNode p = head; p != null; p = p.next) {
13-
if (node == null || node.val == p.val) {
14-
node = p;
15-
count++;
16-
} else {
17-
if (count == 1) {
18-
cur.next = node;
19-
cur = cur.next;
20-
}
21-
node = p;
22-
count = 1;
23-
}
24-
}
25-
26-
if (node != null && count == 1) {
27-
cur.next = node;
28-
cur = cur.next;
29-
}
30-
31-
cur.next = null;
32-
return dummy.next;
33-
}
34-
35-
ListNode make(int[] arr) {
36-
ListNode dummy = new ListNode(0), cur = dummy;
37-
for (int n : arr) {
38-
cur.next = new ListNode(n);
39-
cur = cur.next;
6+
int reversePairs(int[] arr, int m) {
7+
int pairs = 0;
8+
for (int i = m - 1, j = arr.length - 1; j >= m; j--) {
9+
for ( ; i >= 0 && arr[i] > arr[j]; i--);
10+
pairs += m - 1 - i;
4011
}
41-
return dummy.next;
12+
return pairs;
4213
}
43-
4414
}
4515

4616

4717
public static void main(String[] args) {
4818
Solution s = new Solution();
4919
int[] arr = {
50-
1, 3, 3, 3, 1, 2, 4, 4, 1
20+
1, 4, 8, 9, 2, 3, 6, 7
5121
};
52-
ListNode head = s.make(arr);
53-
head = s.removeDup(head);
54-
for (ListNode p = head; p != null; p = p.next) {
55-
System.out.print(p.val + " ");
56-
}
22+
int n = s.reversePairs(arr, 4);
23+
System.out.println(n);
5724
}
5825
}

0 commit comments

Comments
 (0)