Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.

Example:

Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3

Hint: The number of elements in the given matrix will not exceed 10,000.

这道题给了我们一个二维矩阵,让我们求矩阵中最长的连续1,连续方向任意,可以是水平,竖直,对角线或者逆对角线均可。那么最直接最暴力的方法就是四个方向分别来统计最长的连续1,其中水平方向和竖直方向都比较容易,就是逐行逐列的扫描,使用一个计数器,如果当前位置是1,则计数器自增1,并且更新结果res,否则计数器清零。对于对角线和逆对角线需要进行些坐标转换,对于一个mxn的矩阵,对角线和逆对角线的排数都是m+n-1个,难点在于我们要确定每一排上的数字的坐标,如果i是从0到m+n-1之间遍历,j是在i到0之间遍历,那么对角线的数字的坐标就为(i-j, j),逆对角线的坐标就为(m-1-i+j, j),这是博主千辛万苦试出来的T.T,如果能直接记住,效果肯定棒!那么有了坐标转换,求对角线和逆对角线的连续1也就不是啥难事了,参见代码如下:

解法一:

class Solution {
public:
int longestLine(vector<vector<int>>& M) {
if (M.empty() || M[].empty()) return ;
int res = , m = M.size(), n = M[].size();
for (int i = ; i < m; ++i) { // Check horizontal
int cnt = ;
for (int j = ; j < n; ++j) {
if (M[i][j] == ) res = max(res, ++cnt);
else cnt = ;
}
}
for (int j = ; j < n; ++j) {
int cnt = ;
for (int i = ; i < m; ++i) { // Check vertical
if (M[i][j] == ) res = max(res, ++cnt);
else cnt = ;
}
}
for (int i = ; i < m + n - ; ++i) {
int cnt1 = , cnt2 = ;
for (int j = i; j >= ; --j) {
if (i - j < m && j < n) { // Check diagonal
if (M[i - j][j] == ) res = max(res, ++cnt1);
else cnt1 = ;
}
int t = m - - i + j;
if (t >= && t < m && j < n ) { // Check anti-diagonal
if(M[t][j] == ) res = max(res, ++cnt2);
else cnt2 = ;
}
}
}
return res;
}
};

如果上面的解法的坐标转换不好想的话,我们也可以考虑用DP解法来做,我们建立一个三维dp数组,其中dp[i][j][k]表示从开头遍历到数字nums[i][j]为止,第k种情况的连续1的个数,k的值为0,1,2,3,分别对应水平,竖直,对角线和逆对角线这四种情况。之后就是更新dp数组的过程了,如果如果数字为0的情况直接跳过,然后水平方向就加上前一个的dp值,竖直方向加上上面一个数字的dp值,对角线方向就加上右上方数字的dp值,逆对角线就加上左上方数字的dp值,然后每个值都用来更新结果res,参见代码如下:

解法二:

class Solution {
public:
int longestLine(vector<vector<int>>& M) {
if (M.empty() || M[].empty()) return ;
int m = M.size(), n = M[].size(), res = ;
vector<vector<vector<int>>> dp(m, vector<vector<int>>(n, vector<int>()));
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (M[i][j] == ) continue;
for (int k = ; k < ; ++k) dp[i][j][k] = ;
if (j > ) dp[i][j][] += dp[i][j - ][]; // horizonal
if (i > ) dp[i][j][] += dp[i - ][j][]; // vertical
if (i > && j < n - ) dp[i][j][] += dp[i - ][j + ][]; // diagonal
if (i > && j > ) dp[i][j][] += dp[i - ][j - ][]; // anti-diagonal
res = max(res, max(dp[i][j][], dp[i][j][]));
res = max(res, max(dp[i][j][], dp[i][j][]));
}
}
return res;
}
};

下面我们来优化空间复杂度,用一种类似于DFS的思路来解决问题,我们在遍历到为1的点时,对其水平方向,竖直方向,对角线方向和逆对角线方向分别不停遍历,直到越界或者遇到为0的数字,同时用计数器来累计1的个数,这样就可以用来更新结果res了,就不用把每个中间结果都保存下来了,参见代码如下:

解法三:

class Solution {
public:
int longestLine(vector<vector<int>>& M) {
if (M.empty() || M[].empty()) return ;
int m = M.size(), n = M[].size(), res = ;
vector<vector<int>> dirs{{,},{,},{-,-},{-,}};
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (M[i][j] == ) continue;
for (int k = ; k < ; ++k) {
int cnt = , x = i, y = j;
while (x >= && x < m && y >= && y < n && M[x][y] == ) {
x += dirs[k][];
y += dirs[k][];
++cnt;
}
res = max(res, cnt);
}
}
}
return res;
}
};

参考资料:

https://discuss.leetcode.com/topic/87231/dfs-straightforward

https://discuss.leetcode.com/topic/87197/java-o-nm-time-dp-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1的更多相关文章

  1. LC 562. Longest Line of Consecutive One in Matrix

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  2. LeetCode 562. Longest Line of Consecutive One in Matrix(在矩阵中最长的连续1)$

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  3. Longest Line of Consecutive One in Matrix

    Given a 01 matrix, find the longest line of consecutive 1 in the matrix. The line could be horizonta ...

  4. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  5. 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. LeetCode——Longest Consecutive Sequence

    LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...

随机推荐

  1. KVM之二:配置网络

    1.安装KVM a.通过yum安装虚拟化的软件包 [root@kvm ~ ::]#yum install -y kvm virt-* libvirt bridge-utils qemu-img 说明: ...

  2. 【HTTP协议】---HTTP协议详解

    HTTP协议详解 一.HTTP简介 1.HTTP协议,即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和万维网(WWW = World Wide We ...

  3. web api 安全设计(1)

    环境:后台 ASP.NET Web API ,前端为 html,js(跨域访问) 场景1: 客户端自保管RSA 公钥和密钥,签名为客户端私钥签名,服务端用客户端公钥进行签名验证 场景2: 客户端使用S ...

  4. Spring基于注解开发异常

    基于注解开发: 一开始:用的jar包: 百度查到: 导入aop包: 没用 有的说: Spring版本和jdk版本不匹配 于是我换成了4.0版本 导入的jar包: 还是报错. 解决办法:添加spring ...

  5. Jquery($第一天)【历史】

    一.什么是jQueryjQuery是一个JavaScript库,它通过封装原生的JavaScript函数得到一整套定义好的方法.它的作者是John Resig,于2006年创建的一个开源项目,随着越来 ...

  6. 关于DLL的创建与使用简单描述(C++、C#)

    前言 前一段时间在学关于DLL的创建与调用,结果发现网络上一大堆别人分享的经验都有点问题.现在整理分享一下自己的方法. 工具 Microsoft Visual Studio 2017 depends ...

  7. 201421123042 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  8. Webview之H5页面调用android的图库及文件管理

    h5页面打开图片管理器 一般页面在pc打开文件管理器是用 type="file"的代码,可是这在android的webview是无效的,必须为webview设定WebChromeC ...

  9. 几种Java的JSON解析库速度对比

    java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考 了.我们很少会去 ...

  10. 在wamp集成环境下安装laravel5.2.*框架

    虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...