leetcode773
使用两种语言实现,先贴C++的
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int row = image.size();
int col = image[].size();
int oldColor = image[sr][sc];
if (oldColor == newColor)
{
return image;
}
queue<pair<int, int>> Q;
Q.push(make_pair(sr, sc));
const int N = ;
int Visited[N][N];
for (int i = ; i < row; i++)
{
for (int j = ; j < col; j++)
{
Visited[i][j] = false;
}
}
while (!Q.empty())
{
pair<int, int> Node = Q.front();
Q.pop();
int x = Node.first;
int y = Node.second;
if (Visited[x][y])
{
continue;//略过此点
}
else
{
Visited[x][y] = true;
image[x][y] = newColor;
}
pair<int, int> Up_Node = make_pair(x - , y);
pair<int, int> Down_Node = make_pair(x + , y);
pair<int, int> Left_Node = make_pair(x, y - );
pair<int, int> Right_Node = make_pair(x, y + );
if (Up_Node.first >= && !Visited[Up_Node.first][Up_Node.second] && image[Up_Node.first][Up_Node.second] == oldColor)
{
Q.push(Up_Node);
}
if (Down_Node.first <= row - && !Visited[Down_Node.first][Down_Node.second] && image[Down_Node.first][Down_Node.second] == oldColor)
{
Q.push(Down_Node);
}
if (Left_Node.second >= && !Visited[Left_Node.first][Left_Node.second] && image[Left_Node.first][Left_Node.second] == oldColor)
{
Q.push(Left_Node);
}
if (Right_Node.second <= col - && !Visited[Right_Node.first][Right_Node.second] && image[Right_Node.first][Right_Node.second] == oldColor)
{
Q.push(Right_Node);
}
}
return image;
}
};
下面贴出C#的
public class Solution
{
public int[,] FloodFill(int[,] image, int sr, int sc, int newColor)
{
int row = image.GetLength();
int col = image.GetLength(); int oldColor = image[sr, sc];
if (oldColor == newColor)
{
return image;
}
Queue<KeyValuePair<int, int>> Q = new Queue<KeyValuePair<int, int>>();
Q.Enqueue(new KeyValuePair<int, int>(sr, sc)); bool[,] Visited = new bool[row, col]; while (Q.Any())
{
var Node = Q.Dequeue();
int x = Node.Key;
int y = Node.Value;
if (Visited[x, y])
{
continue;//略过此点
}
else
{
Visited[x, y] = true;
image[x, y] = newColor;
}
var Up_Node = new KeyValuePair<int, int>(x - , y);
var Down_Node = new KeyValuePair<int, int>(x + , y);
var Left_Node = new KeyValuePair<int, int>(x, y - );
var Right_Node = new KeyValuePair<int, int>(x, y + ); if (Up_Node.Key >= && !Visited[Up_Node.Key, Up_Node.Value] && image[Up_Node.Key, Up_Node.Value] == oldColor)
{
Q.Enqueue(Up_Node);
} if (Down_Node.Key <= row - && !Visited[Down_Node.Key, Down_Node.Value] && image[Down_Node.Key, Down_Node.Value] == oldColor)
{
Q.Enqueue(Down_Node);
} if (Left_Node.Value >= && !Visited[Left_Node.Key, Left_Node.Value] && image[Left_Node.Key, Left_Node.Value] == oldColor)
{
Q.Enqueue(Left_Node);
} if (Right_Node.Value <= col - && !Visited[Right_Node.Key, Right_Node.Value] && image[Right_Node.Key, Right_Node.Value] == oldColor)
{
Q.Enqueue(Right_Node);
}
} return image;
}
}
leetcode773的更多相关文章
- [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
随机推荐
- Cassandra二级索引原理——新创建了一张表格,同时将原始表格之中的索引字段作为新索引表的Primary Key,并且存储的值为原始数据的Primary Key,然后再通过pk一级索引找到真正的值
1.什么是二级索引? 我们前面已经介绍过Cassandra之中有各种Key,比如Primary Key, Cluster Key 等等.如果您对这部分概念并不熟悉,可以参考之前的文章: [Cassan ...
- LeetCode OJ:Spiral MatrixII(螺旋矩阵II)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Linux下windows中文文本文件乱码问题
table of content: 乱码问题 用gedit选择正确的字符编码打开文件 文件转码 总结 §乱码 Fedora安装时默认用UTF-8字符编码方式, 这么做有国际化的好处(和很多用utf-8 ...
- Java局域网对战游戏、天气预报项目
功能 1.天气预报 2.局域网对战 展示 java学习群669823128 部分源码 package game.weather; import java.util.HashMap; public ...
- 为什么选择MpVue进行小程序的开发
前言 mpvue是一款使用Vue.js开发微信小程序的前端框架.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为H5和小程序提供了代码复用的能力.如果想将 H5 项目改造为小程序,或开发 ...
- 解决Net内存泄露原因
Net内存泄露原因及解决办法 https://blog.csdn.net/changtianshuiyue/article/details/52443821 什么是.Net内存泄露 (1).NET 应 ...
- 使用阿里云Code进行版本控制并配置IDEA
1. 申请阿里code的账号,网址如下https://code.aliyun.com, 2. 申请完成之后,将账号信息发给项目负责人,由负责人加入项目中 3. 下载git,下载地址为 ...
- 剑指offer-第四章解决面试题的思路(包含min函数的栈)
题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...
- bootstrap table 兼容ie8 -- refreshOptions
今天项目使用 bootstrap table 在ie8下发现 方法 refreshOptions 报错. 经过调试监控发现错误如下: 153 行 代码 Object.getOwnPropertyNam ...
- 无敌JS关闭当前窗口代码,不弹出确认提示
echo "<script type='text/javascript'>window.opener=null;window.open('','_self');window.cl ...