• 【Leetcode_easy】733. Flood Fill


    problem

    733. Flood Fill

    题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法、八邻域像素填充法、基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈)。

    泛洪填充算法原理:从某个像素点开始,将封闭区域内的所有此像素值位置的元素填充为新颜色。

    solution1: 递归方法;

    class Solution {
    public:
        vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
            if(image[sr][sc] == newColor) return image;
            helper(image, sr, sc, image[sr][sc], newColor);
            return image;
        }
        void helper(vector<vector<int>>& image, int i, int j, int color, int newColor) {
            int m = image.size(), n = image[0].size();
            if(i<0 || i>=m || j<0 || j>=n || image[i][j]!=color) return;//errr..
            image[i][j] = newColor;
            helper(image, i-1, j  , color, newColor);
            helper(image, i+1, j  , color, newColor);
            helper(image, i  , j-1, color, newColor);
            helper(image, i  , j+1, color, newColor);
        }
    };

    solution2: 非递归方法;

    参考

    1. Leetcode_easy_733. Flood Fill;

    2. Grandyang;

  • 相关阅读:
    java堆
    本地方法栈
    java虚拟机栈
    Java 程序计数器
    面向对象 基本概念 复习
    if __name__=='__main__'
    偏函数与模块
    可变参数与关键字参数(复习材料)
    匿名函数
    闭包
  • 原文地址:https://www.cnblogs.com/happyamyhope/p/11115150.html
Copyright © 2020-2023  润新知