class Solution {
public:
void dfs(vector<vector<int>>& image,int sr, int sc, int newColor,int oldColor){
if(sr < 0 || sr >= image.size() || sc < 0 || sc >= image[sr].size() || image[sr][sc] != oldColor){
return;
}
//涂色+四个方向扩散
image[sr][sc] = newColor;
dfs(image, sr - 1, sc, newColor, oldColor);
dfs(image, sr + 1, sc, newColor, oldColor);
dfs(image, sr , sc - 1, newColor, oldColor);
dfs(image, sr , sc + 1, newColor, oldColor);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(newColor == image[sr][sc]){
return image;
}
dfs(image,sr,sc,newColor,image[sr][sc]);
return image;
}
};
额
class Solution {
public:
void dfs(vector<vector<int>>& image,int sr, int sc, int newColor,int oldColor,vector<vector<int>>flag){
if(flag[sr][sc]==1)return;
image[sr][sc]=newColor;
if(sr>0&&image[sr-1][sc]!=oldColor)dfs(image,sr-1,sc,newColor,oldColor,flag);
if(sr<image.size()&&image[sr+1][sc]!=oldColor)dfs(image,sr+1,sc,newColor,oldColor,flag);
if(sc>0&&image[sr][sc-1]!=oldColor)dfs(image,sr,sc-1,newColor,oldColor,flag);
if(sr<image[0].size()&&image[sr][sc+1]!=oldColor)dfs(image,sr,sc+1,newColor,oldColor,flag);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int m=image.size();
int n=image[0].size();
vector<vector<int>>flag(n,vector<int>(m,0));
if(newColor == image[sr][sc]){
return image;
}
dfs(image,sr,sc,newColor,image[sr][sc],flag);
return image;
}
};
一旦编译出现AddressSanitizer: stack-buffer-overflow on address 错误,首先就检查自己代码的边界条件是否没有控制住。
什么是stack-buffer-overflow ?
举例来说:
int a[10];
a[11] = 10; // stack-buffer-overflow
a[-1] = 10;//stack-buffer-underflow
什么是head-buffer-overflow ?
举例来说:
int* x = malloc(10);
int n=x[11]; //heap-buffer-overflow
int n=x[-1] //heap-buffer-underflow
class Solution {
public:
void dfs(vector<vector<int>>& image,int sr, int sc, int newColor,int oldColor){
//if(flag[sr][sc]==1)return;
image[sr][sc]=newColor;
//flag[sr][sc]=1;
if(sr>0&&image[sr-1][sc]==oldColor)dfs(image,sr-1,sc,newColor,oldColor);
if(sr<image.size()-1&&image[sr+1][sc]==oldColor)dfs(image,sr+1,sc,newColor,oldColor);
if(sc>0&&image[sr][sc-1]==oldColor)dfs(image,sr,sc-1,newColor,oldColor);
if(sc<image[0].size()-1&&image[sr][sc+1]==oldColor)dfs(image,sr,sc+1,newColor,oldColor);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
//int m=image.size();
//int n=image[0].size();
//vector<vector<int>>flag(m,vector<int>(n,0));
if(newColor == image[sr][sc]){
return image;
}
dfs(image,sr,sc,newColor,image[sr][sc]);
return image;
}
};
class Solution {
public:
void dfs(vector<vector<int>>& image,int sr, int sc, int newColor,int oldColor,vector<vector<int>>flag){
if(flag[sr][sc]==1)return;
image[sr][sc]=newColor;
flag[sr][sc]=1;
if(sr>0&&image[sr-1][sc]==oldColor)dfs(image,sr-1,sc,newColor,oldColor,flag);
if(sr<image.size()-1&&image[sr+1][sc]==oldColor)dfs(image,sr+1,sc,newColor,oldColor,flag);
if(sc>0&&image[sr][sc-1]==oldColor)dfs(image,sr,sc-1,newColor,oldColor,flag);
if(sc<image[0].size()-1&&image[sr][sc+1]==oldColor)dfs(image,sr,sc+1,newColor,oldColor,flag);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int m=image.size();
int n=image[0].size();
vector<vector<int>>flag(m,vector<int>(n,0));
if(newColor == image[sr][sc]){
return image;
}
dfs(image,sr,sc,newColor,image[sr][sc],flag);
return image;
}
};
额,怎么没把时间加速。。