C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3752 访问。
3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等。
给定一个由整数组成的 m * n 矩阵,其中有多少个 3 × 3 的 “幻方” 子矩阵?(每个子矩阵都是连续的)。
输入: [[4,3,8,4],
[9,5,1,9],
[2,7,6,2]]输出: 1
解释:
下面的子矩阵是一个 3 x 3 的幻方:
438
951
276而这一个不是:
384
519
762总的来说,在本示例所给定的矩阵中只有一个 3 x 3 的幻方子矩阵。
提示:
1 <= grid.length = grid[0].length <= 10
0 <= grid[i][j] <= 15
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.
Given an grid of integers, how many 3 x 3 "magic square" subgrids are there? (Each subgrid is contiguous).
Input: [[4,3,8,4],
[9,5,1,9],
[2,7,6,2]]Output: 1
Explanation:
The following subgrid is a 3 x 3 magic square:
438
951
276while this one is not:
384
519
762In total, there is only one magic square inside the given grid.
Note:
1 <= grid.length <= 10
1 <= grid[0].length <= 10
0 <= grid[i][j] <= 15
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3752 访问。
public class Program {
public static void Main(string[] args) {
int[][] nums = null;
nums = new int[3][] {new int[]{4,3,8,4},
new int[] {9,5,1,9},
new int[] {2,7,6,2}
};
var res = NumMagicSquaresInside(nums);
Console.WriteLine(res);
nums = new int[3][] {new int[]{1,0,3,8},
new int[] {3,7,2,4},
new int[] {5,8,1,0}
};
res = NumMagicSquaresInside2(nums);
Console.WriteLine(res);
Console.ReadKey();
}
private static int NumMagicSquaresInside(int[][] grid) {
var num = 0;
for(var i = 1; i < grid.Length - 1; i++) {
for(var j = 1; j < grid[i].Length - 1; j++) {
if(grid[i][j] == 5) {
num = IsMagicSquare(grid, i, j) ? ++num : num;
}
}
}
return num;
}
private static bool IsMagicSquare(int[][] grid, int i, int j) {
//原始解法,不推荐
//记录每个值,看是不是9位,因为从1-9都必须出现
var dic = new Dictionary<int, int>();
dic[grid[i - 1][j - 1]] = grid[i - 1][j - 1];
dic[grid[i - 1][j]] = grid[i - 1][j];
dic[grid[i - 1][j + 1]] = grid[i - 1][j + 1];
dic[grid[i][j - 1]] = grid[i][j - 1];
dic[grid[i][j]] = grid[i][j];
dic[grid[i][j + 1]] = grid[i][j + 1];
dic[grid[i + 1][j - 1]] = grid[i + 1][j - 1];
dic[grid[i + 1][j]] = grid[i + 1][j];
dic[grid[i + 1][j + 1]] = grid[i + 1][j + 1];
//不为9或不在1-9范围之内,则不是幻方
if(dic.Count != 9) return false;
foreach(var item in dic) {
if(item.Value > 9 || item.Value < 0) return false;
}
//记录3行、3列、2对角线
var sum_row1 = grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1];
var sum_row2 = grid[i][j - 1] + grid[i][j] + grid[i][j + 1];
var sum_row3 = grid[i + 1][j - 1] + grid[i + 1][j] + grid[i + 1][j + 1];
var sum_col1 = grid[i - 1][j - 1] + grid[i][j - 1] + grid[i + 1][j - 1];
var sum_col2 = grid[i - 1][j] + grid[i][j] + grid[i + 1][j];
var sum_col3 = grid[i - 1][j + 1] + grid[i][j + 1] + grid[i + 1][j + 1];
var sum_cross1 = grid[i - 1][j - 1] + grid[i][j] + grid[i + 1][j + 1];
var sum_cross2 = grid[i - 1][j + 1] + grid[i][j] + grid[i + 1][j - 1];
var dic2 = new Dictionary<int, int>();
dic2[sum_row1] = 0;
dic2[sum_row2] = 0;
dic2[sum_row3] = 0;
dic2[sum_col1] = 0;
dic2[sum_col2] = 0;
dic2[sum_col3] = 0;
dic2[sum_cross1] = 0;
dic2[sum_cross2] = 0;
//看值是不是相同并且值之和为15
return dic2.Count == 1 && sum_row1 == 15;
}
private static int NumMagicSquaresInside2(int[][] grid) {
var num = 0;
for(var i = 1; i < grid.Length - 1; i++) {
for(var j = 1; j < grid[i].Length - 1; j++) {
if(grid[i][j] == 5) {
num = IsMagicSquare(grid, i, j) ? ++num : num;
}
}
}
return num;
}
private static bool IsMagicSquare2(int[][] grid, int row, int col) {
//值必须是1-9
for(var i = row - 1; i <= row + 1; i++)
for(var j = col - 1; j <= col + 1; j++)
if(grid[i][j] < 1 || grid[i][j] > 9) return false;
//不考虑中间的5,只需要考虑4个位置的值的和为10即可
return !(grid[row - 1][col - 1] + grid[row + 1][col + 1] != 10 ||
grid[row - 1][col] + grid[row + 1][col] != 10 ||
grid[row - 1][col + 1] + grid[row + 1][col - 1] != 10 ||
grid[row][col - 1] + grid[row][col + 1] != 10);
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3752 访问。
1
0
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)的更多相关文章
- [Swift]LeetCode840. 矩阵中的幻方 | Magic Squares In Grid
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...
- leetcode刷题-54螺旋矩阵
题目 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 思路 对于每个外层,从左上方开始以顺时针的顺序遍历所有元素.假设当前层的左上角位于(to ...
- Leetcode刷题之螺旋矩阵
矩阵之螺旋矩阵 总体思路: 注意遍历顺序 每次遍历一圈时候不要多加元素 Leetcode54螺旋矩阵 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素. ...
- leetcode刷题-59螺旋矩阵2
题目 给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 思路 与螺旋矩阵题完全一致 实现 class Solution: def generateM ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- C#LeetCode刷题之#532-数组中的K-diff数对(K-diff Pairs in an Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3716 访问. 给定一个整数数组和一个整数 k, 你需要在数组里找 ...
- C#LeetCode刷题之#720-词典中最长的单词(Longest Word in Dictionary)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4120 访问. 给出一个字符串数组words组成的一本英语词典.从 ...
- leetcode刷题-94二叉树的中序遍历
题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
随机推荐
- 史上最全的 jmeter 获取 jdbc 数据使用的4种方法——(软件测试Python自动化)
周五,下班了吗?软件测试人. 明天是周末了!给大家推荐一个技术干货好文.史上最全的 jmeter 获取 jdbc 数据使用的四种方法.我也精剪了jmeter的自动化接口测试的视频放在了同名UP主,周末 ...
- Ethical Hacking - NETWORK PENETRATION TESTING(13)
Nmap Nmap is a network discovery tool that can be used to gather detailed information about any clie ...
- 2019CSP-J T4 加工零件
题目描述 凯凯的工厂正在有条不紊地生产一种神奇的零件,神奇的零件的生产过程自然也很神奇.工厂里有 n 位工人,工人们从 1 ∼n 编号.某些工人之间存在双向的零件传送带.保证每两名工人之间最多只存在一 ...
- 耐心看,1个Dubbo漏洞,35道必问面试题,Dubbo没什么可神秘的
Dubbo漏洞 无意中在网上看到了这样的一条新闻,说是我们360监测发现了Dubbo官方发布的危险漏洞通告,而且尴尬的是,世界上受影响最大的居然是中国,有图有真相 我感觉这也从侧面证明了一件事情,就是 ...
- 服务注册与发现【Eureka】- Eureka简介
什么是服务治理 SpringCloud 封装了 Netflix 公司开发的 Eureka 模块来 实现服务治理. 在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所 ...
- WPF 有缩放时显示线条的问题
公司项目已经开发好几年了,用的WPF开发的,期间遇到好多问题,都是些小细节.很久没有写博客了,以后有时间还是需要写写博客啊!作为分享也好.记录也好,利人利己嘛. 今天主要说一下显示线条的问题,因为我们 ...
- Mybatis——Mapper解析
Mapper的注册入口在Configuration的addMapper方法中,其会调用MapperRegistry的addMapper方法. Mapper的注册过程分为两个步骤: 1.创建Mapper ...
- 毫无基础的人入门Python,Python新手入门教程2
1.6 面向对象和内存分析086.面向对象和面向过程的区别_执行者思维_设计者思维087.对象的进化故事088.类的定义_类和对象的关系089.构造函数__init__090.实例属性_内存分析091 ...
- $0.\dot{9}=1,是指以1为极限,而非初等数学的相等“=”$
$注:文中的讨论,没有使用严格的 \epsilon 极限定义,而是简单假设$ 按照中小学的定义,整数,有限小数,无限循环小数是有理数.无限不循环小数是无理数. $\frac{1}{3}=0.\dot{ ...
- VSCode package.json warning: Problems loading reference 'https://json.schemastore.org/package'...
报错内容 Problems loading reference 'https://json.schemastore.org/package': Unable to load schema from ' ...