解題心得
如果一開始 image[sr][sc] 的顏色就跟 color 一樣,就不要往下了。卡在這邊好久。
程式碼
class Solution { public: void helper(vector<vector<int>>& v, int target, int color, int sr, int sc) { if(!(0 <= sr && sr < v.size() && 0 <= sc && sc < v[0].size())) return; if(v[sr][sc] == target) { v[sr][sc] = color; helper(v, target, color, sr - 1, sc); helper(v, target, color, sr + 1, sc); helper(v, target, color, sr, sc - 1); helper(v, target, color, sr, sc + 1); } } vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { if(image[sr][sc] != color) helper(image, image[sr][sc], color, sr, sc); return image; } };
沒有留言:
張貼留言