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 ...
随机推荐
- JS JQuery 操作: Json转 Excel 下载文件
方法的调用 var json = '[' + '{"申请流水号":"123456","保险公司":"测试数据",&quo ...
- NOIP1999提高组 题解报告
T1 导弹拦截 题目大意:依次有\(n\) (\(n \le 10^5\))枚导弹,一套导弹拦截系统只能拦截一系列高度递减的导弹(一套系统拦截的弹道不一定相邻).求一套系统最多能拦截多少导弹,以及最少 ...
- Java生成不重复的随机数组的方法
一.JAVA中生成随机数的方式 1.在j2se中使用Math.random()令系统随机选取一个01之间的double类型小数,将其乘以一个数,比如25,就能得到一个025范围内的随机数,这个在j2m ...
- meshing-simple_block
原视频下载地址:https://yunpan.cn/cqjeSzP7s93Pc 访问密码 aaff
- 【原】Python基础-字典
字典是Python唯一内建的映射类型.键可以是数字,字符串和元组. 1 字典的创建 方法一:直接创建 例如: >>> dict = {'key1':'value1', 20: 80} ...
- Java 多线程编程(锁优化)
转:https://mp.weixin.qq.com/s/lDuguEhuWiLY8ofBRy3tZA 并发环境下进行编程时,需要使用锁机制来同步多线程间的操作,保证共享资源的互斥访问. 加锁会带来性 ...
- 【Oracle/Java】批量删除16张十万数据的表 单线程耗时45秒 多线程耗时38秒
昨天做了插入的单线程多线程比较,今天做个删除的. 单线程批量删除类代码: package com.hy.delete.singlethread; import java.sql.Connection; ...
- android studio 错误汇总以及解决办法
android studio 错误汇总以及解决办法 参考 https://www.jianshu.com/p/7c7de6562231 问题1. Error:Execution failed for ...
- js的Map实例
1.创建实例 let map= new Map(); // 创建 2.对map的写入 // 要添加的对象 let obj1 = {name:'张三', sex:'boy',age: 21}; let ...
- 机器学习 - 算法 - SVM 支持向量机
SVM 原理引入 支持向量机( SVM,Support Vector Machine ) 背景 2012年前较为火热, 但是在12年后被神经网络逼宫, 由于应用场景以及应用算法的不同, SVM还是需要 ...