130. Surrounded area
class Solution {
int index[4][2] = {
-1, 0, 0, -1, 1, 0, 0, 1};
void dfs(vector<vector<char>>& board, int x, int y) {
board[x][y] = 'A';
for (int i = 0; i < 4; i++) {
int curx = x + index[i][0];
int cury = y + index[i][1];
if (curx < 0 || curx >= board.size() || cury < 0 || cury >= board[0].size()) continue;
if (board[curx][cury] == 'O') dfs(board, curx, cury);
}
}
public:
void solve(vector<vector<char>>& board) {
int n = board.size(), m = board[0].size();
// 从边界上的 'O' 出发,进行深度优先搜索,将与边界相连的 'O' 标记为 'A'
for (int i = 0; i < m; i++) {
if (board[0][i] == 'O') dfs(board, 0, i);
if (board[n - 1][i] == 'O') dfs(board, n - 1, i);
}
for (int i = 0; i < n; i++) {
if (board[i][0] == 'O') dfs(board, i, 0);
if (board[i][m - 1] == 'O') dfs(board, i, m - 1);
}
// 再次遍历整个矩阵,将 'O' 修改为 'X',将 'A' 恢复为 'O'
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
//这里顺序不能搞混,不然改过的东西还要再改
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == 'A') board[i][j] = 'O';
}
}
}
};
Linked List Basics
handwritten linked list
struct ListNode{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){
}
};
Singly linked list Double linked list Circular linked list
Linked list addresses are not continuous
203. Remove linked list elements
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode*_head=new ListNode();
_head->next=head;
ListNode*cur=_head;
while(cur!= nullptr&&cur->next!=nullptr){
if(cur->next->val==val){
cur->next=cur->next->next;
}
else cur=cur->next;
}
return _head->next;
}
};
707. Design Linked List
class MyLinkedList {
struct ListNode{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){
}
};
ListNode*_head;
int size;
public:
MyLinkedList() {
_head=new ListNode(0);
size=0;
}
int get(int index) {
ListNode*cur=_head;
if(index>size-1||index<0)return -1;
while(index--){
cur=cur->next;
}
return cur->next->val;
}
void addAtHead(int val) {
ListNode* head=new ListNode(val);
head->next=_head->next;
_head->next=head;
size++;
}
void addAtTail(int val) {
ListNode*cur=_head;
while(cur->next!=NULL){
cur=cur->next;
}
ListNode*node=new ListNode(val);
cur->next=node;
size++;
}
void addAtIndex(int index, int val) {
//这里是我犯的错误,应该细心一点具体问题具体分析,
if(index>size)return;
if(index<0)index=0;
ListNode*node=new ListNode(val);
ListNode*cur=_head;
while(index--){
cur=cur->next;
}
ListNode*tmp=cur->next;
cur->next=node;
node->next=tmp;
size++;
}
void deleteAtIndex(int index) {
if(index>size-1||index<0)return;
ListNode*cur=_head;
while(index--){
cur=cur->next;
}
cur->next=cur->next->next;
size--;
}
};
206. Reverse Linked List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode*cur=nullptr;
ListNode*tmp=head;
while(tmp!=nullptr){
ListNode*tmp2=tmp->next;
tmp->next=cur;
cur=tmp;
tmp=tmp2;
}
return cur;
}
};