Skip to content

Commit ec09983

Browse files
committed
[Function add]
1. Add leetcode solution.
1 parent dca673c commit ec09983

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

leetcode/18. 4Sum.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
## 18. 4Sum
2+
### Question:
3+
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
4+
5+
Note:
6+
7+
* The solution set must not contain duplicate quadruplets.
8+
29
### Thinking:
3-
* Method:
10+
* Method:
411
It is very similar to 3sum, but we use one more index to iterate.
512
It is very important to remove duplicate elements.
613
```Java
@@ -21,7 +28,7 @@ It is very important to remove duplicate elements.
2128
while(left < right && nums[left-1] == nums[left]) left++;
2229
while(left < right && nums[right+1] == nums[right]) right--;
2330
}else if(temp < target){
24-
left++;
31+
left++;
2532
}else{
2633
right--;
2734
}
@@ -33,4 +40,37 @@ It is very important to remove duplicate elements.
3340
return result;
3441
}
3542
}
43+
```
44+
45+
### 二刷
46+
1. 和一刷的时候类似,外围二次遍历,内部使用双指针。
47+
48+
```Java
49+
class Solution {
50+
public List<List<Integer>> fourSum(int[] nums, int target) {
51+
List<List<Integer>> result = new ArrayList<>();
52+
if(nums == null || nums.length < 4) return result;
53+
int low = 0, high = 0, sum = 0;
54+
Arrays.sort(nums);
55+
for(int i = 0; i < nums.length - 3; i++){
56+
if(i > 0 && nums[i] == nums[i - 1]) continue;
57+
for(int j = i + 1; j < nums.length - 2; j++){
58+
if(j - i > 1 && nums[j - 1] == nums[j]) continue;
59+
low = j + 1; high = nums.length - 1;
60+
while(low < high){
61+
sum = nums[i] + nums[j] + nums[low] + nums[high];
62+
if(sum == target){
63+
result.add(Arrays.asList(nums[i], nums[j], nums[low++], nums[high--]));
64+
while(high > low && nums[high] == nums[high + 1]) high--;
65+
while(low < high && nums[low] == nums[low - 1]) low++;
66+
}else if(sum > target){
67+
--high;
68+
}else
69+
++low;
70+
}
71+
}
72+
}
73+
return result;
74+
}
75+
}
3676
```

leetcode/19. Remove Nth Node From End of List.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
## 19. Remove Nth Node From End of List
2+
3+
### Question:
4+
Given a linked list, remove the n-th node from the end of list and return its head.
5+
6+
```
7+
Example:
8+
9+
Given linked list: 1->2->3->4->5, and n = 2.
10+
11+
After removing the second node from the end, the linked list becomes 1->2->3->5.
12+
```
13+
214
### Thinking:
3-
* Method:
15+
* Method:
416
1. Iterate to get the length of the list.
517
2. Iterate again to remove
618
3. Trick: Use a dummy node to represent the head.
@@ -38,4 +50,33 @@
3850
return result.next;
3951
}
4052
}
53+
```
54+
55+
### 二刷
56+
1. 快慢指针,先确定两个指针之间的距离。
57+
2. 同时向后移动指针,定位出倒数第n个结点。
58+
3. 通过改变next指针删除元素。
59+
60+
```Java
61+
/**
62+
* Definition for singly-linked list.
63+
* public class ListNode {
64+
* int val;
65+
* ListNode next;
66+
* ListNode(int x) { val = x; }
67+
* }
68+
*/
69+
class Solution {
70+
public ListNode removeNthFromEnd(ListNode head, int n) {
71+
ListNode fast = head, slow = head;
72+
for(int i = 0; i < n + 1; i++)
73+
fast = fast.next;
74+
while(fast != null){
75+
fast = fast.next;
76+
slow = slow.next;
77+
}
78+
slow.next = slow.next.next;
79+
return head;
80+
}
81+
}
4182
```

0 commit comments

Comments
 (0)