Title

# Flood Fill 图染色

X X X X
X O O X
X X O X
O X X X

X X X X
X X X X
X X X X
O X X X



#define dfs solve
typedef vector<vector<char>> g;
int dx[] = {0,0,-1,1},dy[] = {-1,1,0,0};
class Solution {
public:
void solve(vector<vector<char>> &board) {
int n = board.size();
if(n<1) return;
int m=board[0].size();

for(int i=0;i<n;++i) {
dfs(board,i,0,'*');
dfs(board,i,m-1,'*');
}
for(int i=0;i<m;++i) {
dfs(board,0,i,'*');
dfs(board,n-1,i,'*');
}

for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
if(board[i][j]=='O') {
board[i][j] = 'X';
}else if(board[i][j]=='*') board[i][j] = 'O';
}
}

}

void solve(g& board,int a,int b,char p) {
if(a<0||b<0||a>=board.size()||b>=board[0].size()||board[a][b]=='X') return;
board[a][b] = p;
for(int i=0;i<4;++i) {
int x = a+dx[i],y = b+dy[i];
if(x<0||y<0||x>=board.size()||y>=board[0].size()||board[x][y]=='X'||board[x][y]=='*') continue;
solve(board,x,y,p);
}
}

};



## 最大连续序列的长度

[1000, 4, 2000, 1, 3, 2]

4



class Solution {
public:
/**
*
* @param num int整型vector
* @return int整型
*/
int longestConsecutive(vector<int>& num) {
if(num.size()<=1) return num.size();
// write code here
unordered_set<int>_map(num.begin(),num.end());

int malen=1;
for(int x: num) {
int r=x,l=x;
_map.erase(x);
while(_map.count(l-1)) _map.erase(l-1),l--;
while(_map.count(r+1)) _map.erase(r+1),r++;
malen = max(malen,r-l+1);
}

return malen;
}
};


posted @ 2021-01-25 13:39  .geek  阅读(69)  评论(0编辑  收藏  举报