Skip to content

Commit ed7801a

Browse files
committed
revise
1 parent 55fd193 commit ed7801a

File tree

5 files changed

+125
-16
lines changed

5 files changed

+125
-16
lines changed

Container With Most Water.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
// main idea: find the max of (j - i) * min(ai, aj)
3+
class Solution {
4+
public:
5+
int maxArea(vector<int> &height) {
6+
int i = 0, j = height.size() - 1;
7+
int container = 0;
8+
while(i < j){
9+
container = max(container, (j - i) * min(height[i], height[j]));
10+
(height[i] > height[j])? j-- : i++;
11+
}
12+
return container;
13+
}
14+
};

Partition List.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@
1717
class Solution {
1818
public:
1919
ListNode *partition(ListNode *head, int x) {
20-
ListNode stdummy(-1);
21-
ListNode nddummy(-1);
22-
auto l = &stdummy;
23-
auto r = &nddummy;
24-
for(ListNode *curr = head;curr; curr = curr -> next){
25-
if(curr -> val < x){
26-
l -> next = curr;
27-
l = curr; // move the l pointer
20+
if(head == NULL) return head;
21+
ListNode *dummy1 = new ListNode(-1);
22+
ListNode *dummy2 = new ListNode(-1);
23+
ListNode *node1 = dummy1;
24+
ListNode *node2 = dummy2;
25+
while(head){
26+
if(head -> val < x){
27+
node1 -> next = head;
28+
node1 = node1 -> next;
2829
}else{
29-
r -> next = curr;
30-
r = curr;
30+
node2 -> next = head;
31+
node2 = node2 -> next;
3132
}
33+
head = head -> next;
3234
}
33-
34-
l -> next = nddummy.next;
35-
//nddummy.next = NULL;//stupid mistake. how can this happen??
36-
r -> next = NULL;
37-
return stdummy.next;
35+
node2 -> next = NULL; // end flag
36+
node1 -> next = dummy2 -> next; // point dummy2 to the end of node1 list
37+
return dummy1 -> next;
3838
}
39-
4039
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//Remove Duplicates from Sorted List
2+
class Solution {
3+
public:
4+
ListNode *deleteDuplicates(ListNode *head) {
5+
if(head == NULL) return head;
6+
ListNode *node = head;
7+
while(node -> next){ // we need to compare with the next val, so here we should consider about the next node not node!!
8+
int v = node -> val;
9+
if(node -> next -> val == v){
10+
node -> next = node -> next -> next;
11+
}else{
12+
node = node -> next;
13+
}
14+
}
15+
return head;
16+
}
17+
};
18+
19+
//Remove Duplicates from Sorted List ii
20+
21+
class Solution {
22+
public:
23+
ListNode *deleteDuplicates(ListNode *head) {
24+
if(head == NULL || head -> next == NULL) return head;
25+
ListNode *dummy = new ListNode(-1);
26+
dummy -> next = head;
27+
ListNode *node = dummy;
28+
while(node -> next && node -> next -> next){
29+
if(node -> next -> next -> val == node -> next -> val){
30+
int v = node -> next -> val;
31+
while(node -> next && node -> next -> val == v){
32+
node -> next = node -> next -> next;
33+
}
34+
}else{
35+
node = node -> next;
36+
}
37+
}
38+
return dummy -> next;
39+
}
40+
};

Reverse list.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// reverse a linked list
2+
// reverse a linked list. first thing we need to do is to store the value for the next node
3+
// and then give previous node to the head next, then move head to the next level. meanwhile, prev will be the head compare with the tmp
4+
5+
void reverse(Node *&head){
6+
if(head == NULL) return;
7+
Node *prev = NULL;
8+
Node *tmp = NULL;
9+
while(head){
10+
tmp = head -> next;
11+
head -> next = prev;
12+
prev = head;
13+
head = tmp;
14+
}
15+
16+
17+
//find the middle of the linked list
18+
// we have two pointers and move one step by step, another one two steps each time
19+
ListNode *findMid(ListNode *head){
20+
ListNode slow = head, fast = head -> next;
21+
while(fast != NULL && fast -> next != NULL){ // it has to be fast, it's fast than slow pointer
22+
fast = fast -> next -> next;
23+
slow = slow -> next;
24+
}
25+
return slow;
26+
}
27+
28+
29+
30+

Trapping Rain Water.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int trap(int A[], int n) {
4+
if(n < 2) return 0;
5+
int *left = new int[n];
6+
int *right = new int[n];
7+
// find the max height on the left for each state
8+
left[0] = 0;
9+
for(int i = 1; i < n; i++){
10+
left[i] = max(left[i-1], A[i-1]);
11+
}
12+
// find the max height on the right
13+
right[n -1] = 0;
14+
for(int j = n - 2; j >= 0; j--){
15+
right[j] = max(right[j+1], A[j+1]);
16+
}
17+
int trapWater = 0;
18+
for(int i = 0; i < n; i++){
19+
if(min(left[i], right[i]) - A[i] > 0)
20+
trapWater += min(left[i], right[i]) - A[i];
21+
}
22+
delete left;
23+
delete right;
24+
return trapWater;
25+
}
26+
};

0 commit comments

Comments
 (0)