Skip to content

Commit 2068dfe

Browse files
committed
fd
1 parent d9a96f1 commit 2068dfe

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Facebook面试总结](https://github.com/dingjikerbo/leetcode/blob/master/doc/Facebook.md)
1717
- [Bitset技巧](https://github.com/dingjikerbo/leetcode/blob/master/doc/BitSet.md)
1818
- [Map新接口](https://github.com/dingjikerbo/leetcode/blob/master/doc/Map.md)
19+
- [其它题目](extras.md)
1920

2021
<br/><br/>
2122

extras.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 其它题目
2+
3+
### Google
4+
- [Remove consecutive node from single list](leetcode/src/RemoveDuplicateList.java)

leetcode/solution/src/BinaryTreePostorderTraversal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ public List<Integer> postorderTraversal2(TreeNode root) {
3535
} else {
3636
TreeNode peek = stack.peek();
3737
if (peek.right != null && last != peek.right) {
38-
root = peek.right;
38+
root = peek.right; // 只有这里要设置root
3939
} else {
4040
result.add(peek.val);
4141
last = stack.pop();
42+
// 此时root仍为null
4243
}
4344
}
4445
}

leetcode/solution/src/ContainsDuplicateIII.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public class ContainsDuplicateIII {
1010
/**
1111
* 这题要注意溢出问题,干脆都用long
1212
* 时间复杂度O(nlg(min(n,k))
13-
* for循环走到当前的nums[i]说明之前的都不行,因此只要判断当前
14-
* nums[i]的加入是否可以即可,因此只要判断nums[i]相邻的上下两个数是否在t范围内
13+
* 这里相当于维护了一个window,每次新遍历到一个数,都查看上下相邻的数是否满足条件
1514
*/
1615
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
1716
TreeSet<Long> set = new TreeSet<>();

leetcode/solution/src/MaximumGap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public int maximumGap(int[] nums) {
5353
maxGap = Math.max(maxGap, mins[i] - last);
5454
last = maxs[i];
5555
}
56+
// 最后的max别忘了
5657
maxGap = Math.max(maxGap, max - last);
5758
return maxGap;
5859
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class RemoveDuplicateList {
2+
3+
/**
4+
* 一个链表,删除相邻且重复的
5+
* 如 1, 3, 3, 3, 1, 2, 4, 4,只保留1, 1, 2
6+
*/
7+
public ListNode removeDup(ListNode head) {
8+
ListNode dummy = new ListNode(0), cur = dummy;
9+
10+
ListNode node = null;
11+
int count = 0;
12+
13+
for (ListNode p = head; p != null; p = p.next) {
14+
if (node == null || node.val == p.val) {
15+
node = p;
16+
count++;
17+
} else {
18+
if (count == 1) {
19+
cur.next = node;
20+
cur = cur.next;
21+
}
22+
node = p;
23+
count = 1;
24+
}
25+
}
26+
27+
if (node != null && count == 1) {
28+
cur.next = node;
29+
cur = cur.next;
30+
}
31+
32+
cur.next = null;
33+
return dummy.next;
34+
}
35+
}

leetcode/src/Main.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,58 @@
1-
import com.sun.org.apache.xpath.internal.operations.Bool;
2-
3-
import java.util.*;
4-
import java.util.concurrent.*;
51

62
public class Main {
73

84
public static class Solution {
95

10-
public int myAtoi(String str) {
11-
str = str.trim();
12-
if (str.length() == 0) {
13-
return
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+
}
1424
}
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;
1533
}
1634

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;
40+
}
41+
return dummy.next;
42+
}
43+
44+
}
1745

1846

1947
public static void main(String[] args) {
2048
Solution s = new Solution();
49+
int[] arr = {
50+
1, 3, 3, 3, 1, 2, 4, 4, 1
51+
};
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+
}
2157
}
2258
}

0 commit comments

Comments
 (0)