Skip to content

Commit 33c0298

Browse files
author
Botao Xiao
committed
[Function add]
1. Add leetcode solutions.
1 parent 464603a commit 33c0298

5 files changed

+85
-45
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@
415415

416416
[236. Lowest Common Ancestor of a Binary Tree](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree.md)
417417

418+
[237. Delete Node in a Linked List](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/237.%20Delete%20Node%20in%20a%20Linked%20List.md)
419+
420+
[238. Product of Array Except Self](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/238.%20Product%20of%20Array%20Except%20Self.md)
421+
422+
[239. Sliding Window Maximum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/239.%20Sliding%20Window%20Maximum.md)
423+
418424
## Algorithm(4th_Edition)
419425
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
420426
All java realization codes are placed in different packages.

leetcode/237. Delete Node in a Linked List.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,29 @@ class Solution {
5151
node.next = node.next.next;
5252
}
5353
}
54-
```
54+
```
55+
56+
### 二刷
57+
1. Same as what I did in previous time.
58+
```Java
59+
/**
60+
* Definition for singly-linked list.
61+
* public class ListNode {
62+
* int val;
63+
* ListNode next;
64+
* ListNode(int x) { val = x; }
65+
* }
66+
*/
67+
class Solution {
68+
public void deleteNode(ListNode node) {
69+
ListNode cur = node;
70+
ListNode pre = null;
71+
while(cur.next != null){
72+
cur.val = cur.next.val;
73+
pre = cur;
74+
cur = cur.next;
75+
}
76+
pre.next = null;
77+
}
78+
}
79+
```

leetcode/238. Product of Array Except Self.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,27 @@ class Solution {
3939
return result;
4040
}
4141
}
42-
```
42+
```
43+
44+
### 二刷
45+
* Method 1:
46+
1. We use a same size array to save the production of left side(exclude itself).
47+
2. Then we traverse the array from end to top and time the values saved in the array to save the product of right side.
48+
3. For each number, we actually times the left product with right product(itself is not included).
49+
```Java
50+
class Solution {
51+
public int[] productExceptSelf(int[] nums) {
52+
int len = nums.length;
53+
int[] result = new int[len];
54+
for(int i = 0, temp = 1; i < len; i++){
55+
result[i] = temp;
56+
temp *= nums[i];
57+
}
58+
for(int i = len - 1, temp = 1; i >= 0; i--){
59+
result[i] *= temp;
60+
temp *= nums[i];
61+
}
62+
return result;
63+
}
64+
}
65+
```

leetcode/239. Sliding Window Maximum.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,32 @@ class Solution {
4343
return result;
4444
}
4545
}
46-
```
46+
```
47+
48+
### 二刷
49+
1. Still use priority queue. It's not O(n);
50+
```Java
51+
class Solution {
52+
public int[] maxSlidingWindow(int[] nums, int k) {
53+
if(nums == null || nums.length == 0) return new int[0];
54+
int len = nums.length;
55+
int[] result = new int[len - k + 1];
56+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
57+
@Override
58+
public int compare(Integer a, Integer b){
59+
return b - a;
60+
}
61+
});
62+
for(int i = 0; i < k; i++){
63+
pq.offer(nums[i]);
64+
}
65+
for(int i = k; i <= nums.length; i++){
66+
result[i - k] = pq.peek();
67+
pq.remove(nums[i - k]);
68+
if(i < nums.length)
69+
pq.offer(nums[i]);
70+
}
71+
return result;
72+
}
73+
}
74+
```

leetcode/Conclusion.md

-42
This file was deleted.

0 commit comments

Comments
 (0)