网站插件代码怎么用,施工企业合规管理检查制度,昆明网站制作工具,合肥建设官方网站417. 太平洋大西洋水流问题
水往高处流#xff0c;先记录两个海祥往高处流所能留到的地址#xff0c;之后将他们的合并区域进行输出
static const int dirs[4][2] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};class Solution {
public:vectorvectorint heights;v…417. 太平洋大西洋水流问题
水往高处流先记录两个海祥往高处流所能留到的地址之后将他们的合并区域进行输出
static const int dirs[4][2] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};class Solution {
public:vectorvectorint heights;void dfs(int row, int col, vectorvectorbool 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);}}}vectorvectorint pacificAtlantic(vectorvectorint heights) {this-heights heights;int m heights.size();int n heights[0].size();vectorvectorbool pacific(m, vectorbool(n, false));vectorvectorbool atlantic(m, vectorbool(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);}vectorvectorint 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. 两两交换链表中的节点
在纸上画一画
class Solution {
public:ListNode* swapPairs(ListNode* head) {ListNode*_headnew ListNode(0);_head-nexthead;ListNode*cur_head;while(cur-next!nullptrcur-next-next!nullptr){ListNode*tmpcur-next-next-next;ListNode*tmp1cur-next-next;cur-next-next-nextcur-next;cur-next-nexttmp;cur-nexttmp1;curcur-next-next;}return _head-next;}
};19.删除链表的倒数第N个节点
class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode*_headnew ListNode(0);_head-nexthead;ListNode*cur_head;int size0;while(cur-next!nullptr){curcur-next;size;}sizesize-n;cur_head;while(size--){curcur-next;}cur-nextcur-next-next;return _head-next;}
};还有另外一种写法双指针快慢先让快指针走n然后再让慢指针和快指针一起走当快指针为null的时候删除慢指针返回头节点
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;}
};面试题 02.07. 链表相交
很有意思的解法正常的解法应该是先都过一遍然后看大家的尺寸 然后把双方指针移到短指针开始的位置开始比较相等返回不相等噶
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;}
};