417. Problems of Pacific-Atlantic Currents
The water flows to a high place, first record the addresses where the two Haixiang flows to a high place, and then output their merged area
static const int dirs[4][2] = {
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}};
class Solution {
public:
vector<vector<int>> heights;
void dfs(int row, int col, vector<vector<bool>> & ocean) {
int m = ocean.size();
int n = ocean[0].size();
if (ocean[row][col]) {
return;
}
ocean[row][col] = true;
for (int i = 0; i < 4; i++) {
int newRow = row + dirs[i][0], newCol = col + dirs[i][1];
if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && heights[newRow][newCol] >= heights[row][col]) {
dfs(newRow, newCol, ocean);
}
}
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
this->heights = heights;
int m = heights.size();
int n = heights[0].size();
vector<vector<bool>> pacific(m, vector<bool>(n, false));
vector<vector<bool>> atlantic(m, vector<bool>(n, false));
for (int i = 0; i < m; i++) {
dfs(i, 0, pacific);
}
for (int j = 1; j < n; j++) {
dfs(0, j, pacific);
}
for (int i = 0; i < m; i++) {
dfs(i, n - 1, atlantic);
}
for (int j = 0; j < n - 1; j++) {
dfs(m - 1, j, atlantic);
}
vector<vector<int>> result;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.push_back({
i,j});
}
}
}
return result;
}
};
24. Exchange the nodes in the linked list two by two
draw a picture on paper
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode*_head=new ListNode(0);
_head->next=head;
ListNode*cur=_head;
while(cur->next!=nullptr&&cur->next->next!=nullptr){
ListNode*tmp=cur->next->next->next;
ListNode*tmp1=cur->next->next;
cur->next->next->next=cur->next;
cur->next->next=tmp;
cur->next=tmp1;
cur=cur->next->next;
}
return _head->next;
}
};
19. Delete the last N node of the linked list
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode*_head=new ListNode(0);
_head->next=head;
ListNode*cur=_head;
int size=0;
while(cur->next!=nullptr){
cur=cur->next;
size++;
}
size=size-n;
cur=_head;
while(size--){
cur=cur->next;
}
cur->next=cur->next->next;
return _head->next;
}
};
There is another way of writing, double pointer fast and slow, let the fast pointer go n first, then let the slow pointer and fast pointer go together, delete the slow pointer when the fast pointer is null, and return to the head node
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
while(n-- && fast != NULL) {
fast = fast->next;
}
fast = fast->next; // fast再提前走一步,因为需要让slow指向删除节点的上一个节点
while (fast != NULL) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
// ListNode *tmp = slow->next; C++释放内存的逻辑
// slow->next = tmp->next;
// delete nth;
return dummyHead->next;
}
};
Interview Question 02.07. Linked List Intersection
Very interesting solution, the normal solution should be to go through all of them first, and then look at the size of everyone. Then,
move the pointers of both sides to the position where the short pointer starts to start comparison, and return if they are equal, if they are not equal
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
ListNode *pA = headA, *pB = headB;
while (pA != pB) {
pA = pA == nullptr ? headB : pA->next;
pB = pB == nullptr ? headA : pB->next;
}
return pA;
}
};