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 horizontal, vertical, diagonal or anti-diagonal.
Example 1:
Input:
[[0,1,1,0],
[0,1,1,0],
[0,0,0,1]]
Output: 3
Explanation: (0,1) (1,2) (2,3)
Example 2:
Input: [[0,0],[1,1]]
https://www.cnblogs.com/grandyang/p/6900866.html
Output: 2
我们也可以考虑用DP解法来做,我们建立一个三维dp数组,其中dp[i][j][k]表示从开头遍历到数字nums[i][j]为止,第k种情况的连续1的个数,k的值为0,1,2,3,
分别对应水平,竖直,对角线和逆对角线这四种情况。之后就是更新dp数组的过程了,如果如果数字为0的情况直接跳过,然后水平方向就加上前一个的dp值,竖直方向加上上面
一个数字的dp值,对角线方向就加上右上方数字的dp值,逆对角线就加上左上方数字的dp值,然后每个值都用来更新结果res,参见代码如下:
class Solution2 {
int longestLine(int[][] M) {
if (M == null || M.length == || M[].length == ) return ;
int m = M.length, n = M[].length, res = ;
int[][][] dp = new int[m][n][];
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 = Math.max(res, Math.max(dp[i][j][], dp[i][j][]));
res = Math.max(res, Math.max(dp[i][j][], dp[i][j][]));
}
}
return res;
}
}
Longest Line of Consecutive One in Matrix的更多相关文章
- 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 ...
- 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 ...
- [LeetCode] 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 ...
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)
All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
[LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...
随机推荐
- [Luogu] 奶酪
https://www.luogu.org/problemnew/show/3958 #include <iostream> #include <cstdio> #includ ...
- (6)打造简单OS-内存分页
好长时间没有更新了,最近比较忙...... 内存分页可以放在C代码中,这样比较方便编写!即loader执行完后进入kernel_main函数之后在分配内存分页! 一.地址 讲到内存必然要讲到计算机中经 ...
- NodeJs的Event Loop
我们之前谈过浏览器的Event Loop:https://www.cnblogs.com/amiezhang/p/11349450.html 简单来说,就是每执行一个宏任务,就去执行微任务队列,直到清 ...
- 解决JavaWeb项目报错:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
明明有项目和文件,而且别的项目都可以运行,偏偏这个不能用,报错The origin server did not find a current representation for the targe ...
- angularJs driective指令小实例
做一个下拉菜单,体会指令各参数的作用 html代码 <script type="text/ng-template" id="mydropdown.html" ...
- MySql通过Data恢复数据库数据
公司的服务器开不了机,把硬盘装在其他机器上面,文件是没有丢失,可是数据库开不了了.上网查了下,用DATA恢复了数据. 1.先通过MYSQL目录下的my.ini配置文件找到 datadir的文件夹地址. ...
- Android Studio如何删除一个Module
当你想在Android Studio中删除某个module时,大家习惯性的做法都是选中要删除的module,右键去找delete.但是在Android Studio中你选中module,右键会发现没 ...
- Ubuntu16.04 apache2+php7.0+mysql5.7环境搭建
今天配置一下web环境,很常见的apache+php+mysql的网站环境: 步骤一:安装apache sudo apt install apache2 步骤二:安装php7 1.安装PHP7和响应的 ...
- GO 包相关
1 包编译,eg: 引用pkgtest包 pkgtest包没有任何编译,项目直接导入引用,项目编译时实际是会编译pkgtest并在pkg\windows_386下生成pkgtest.a文件 再编译项目 ...
- Python性能监控
profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和pstats.这些分析器提 ...