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. NVL2 这个函数,

    NVL2(expr1,expr2,expr3)     如果参数表达式expr1值为NULL,则NVL2()函数返回参数表达式expr3的值:如果参数表达式expr1值不为NULL,则NVL2()函数 ...

  2. RDD概念、特性、缓存策略与容错

    一.RDD概念与特性 1. RDD的概念 RDD(Resilient Distributed Dataset),是指弹性分布式数据集.数据集:Spark中的编程是基于RDD的,将原始数据加载到内存变成 ...

  3. C和C++运行库

    一.Windows下动态库 1. 静态函数库 这类库的名字一般是libxxx.lib:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后 ...

  4. 记录python接口自动化测试--pycharm执行测试用例时需要使用的姿势(解决if __name__ == "__main__":里面的程序不生效的问题)(第三目)

    1.只运行某一条case 把光标移动到某一条case后面,然后右键,选择"Run..."来运行程序 此时,pycharm会只运行光标所在位置的这一条case 2.如果想执行全部ca ...

  5. C语言博客作业—指针

    一.PTA实验作业 题目1: 求出数组中最大数和次最大数 1. 本题PTA提交列表 2. 设计思路 定义max表示范围数组中的最大数(初值设为a[0]),z表示找到的元素在数组中的位置: 定义指针*b ...

  6. The sum of numbers form 0 to n.(20.9.2017)

    #include <stdio.h> int main() { int a,b,sum; printf("输入一个数字: "); scanf("%d" ...

  7. Digilent Xilinx USB Jtag cable

    Digilent Xilinx USB Jtag cable 安装环境 操作系统:fedora 20 64bit 源链接:https://wiki.gentoo.org/wiki/Xilinx_USB ...

  8. javascript实现小鸟飞行轨迹

    javascript实现小鸟飞行轨迹 代码如下:

  9. day-4 python多进程编程知识点汇总

    1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...

  10. nyoj 回文字符串

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...