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.

Median的题就是简单一点。

1. DFS没有问题。

2. 怎么保存已经遍历过的点,因为每一个点可能有4个方向,所以可以开一个map记录每一个点的四个方向有没有被经过。

我的思路,Runtime有点慢。

Runtime: 88 ms, faster than 13.53% of C++ online submissions for Longest Line of Consecutive One in Matrix.

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
private:
unordered_map<int, vector<bool>> mp;
int dirs[][] = {{,},{,},{,},{,-}};
int n,m;
public:
int longestLine(vector<vector<int>>& M) {
n = M.size();
m = M[].size();
int ret;
REP(i,n){
REP(j,m){
mp[i*m+j] = {false,false,false,false};
}
}
REP(i,n){
REP(j,m){
if(M[i][j] == ){
dfs(M, i, j, ret);
}
}
}
return ret;
}
bool valid(int x, int y){
return x < n && x >= && y < m && y >= ;
} void dfs(vector<vector<int>>& M, int x, int y, int& ret){
if(x < || y < || x >= n || y >= m) return ;
if(M[x][y] == ) return ;
REP(i, ){
if(mp[x*m+y][i]) continue;
mp[x*m+y][i] = true;
int tmpx = x, tmpy = y, dx = dirs[i][], dy = dirs[i][];
int cnt = ;
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx;tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
tmpx = x, tmpy = y, dx = -dirs[i][], dy = -dirs[i][];
while(valid(tmpx+dx,tmpy+dy) && M[tmpx+dx][tmpy+dy] == ){
tmpx += dx; tmpy += dy;
mp[tmpx*m+tmpy][i] = true;
cnt++;
}
ret = max(ret, cnt);
}
}
};
//[[0,1,1,0],
//[0,1,1,0],
//[0,0,0,1]] int main(){
vector<vector<int>> M {{,,,},{,,,},{,,,}};
return ;
}

另一个思路使用dp,dp的精髓在于前后的状态无关,有点类似马尔可夫链。

从矩阵第一行向下遍历,每一次保存的就是当前点四个方向的最大长度,如果点是1,该方向加1。也是很好的思路。

class Solution {

public:
int longestLine(vector<vector<int>>& M)
{
int r=M.size();
if(r==) return ;
int c=M[].size();
vector<vector<vector<int> > > dp(r,vector<vector<int>> (c,vector<int>(,) ) );
int res=;
for(int i=;i<r;i++)
for(int j=;j<c;j++)
{
if(M[i][j])
{
dp[i][j][]=j?dp[i][j-][]+:;
dp[i][j][]=i?dp[i-][j][]+:;
dp[i][j][]=i&&j?dp[i-][j-][]+:;
dp[i][j][]=i>&&j<c-?dp[i-][j+][]+:;
for(auto x:dp[i][j])
res=max(res,x);
}
} return res;
}
};

LC 562. Longest Line of Consecutive One in Matrix的更多相关文章

  1. 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 ...

  2. [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 ...

  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. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. LC 3. Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  6. LC 967. Numbers With Same Consecutive Differences

    Return all non-negative integers of length N such that the absolute difference between every two con ...

  7. LC 687. Longest Univalue Path

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  8. [LC] 659. Split Array into Consecutive Subsequences

    Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...

  9. [LC] 1048. Longest String Chain

    Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...

随机推荐

  1. angular实现对百度天气api跨域请求

    申请秘钥:http://lbsyun.baidu.com/apiconsole/key  ,有个百度账号就行ak=开发者秘钥 url地址  :http://api.map.baidu.com/tele ...

  2. mac下MySQL出现乱码的解决方法

    之前写过一篇Linux下MySQL出现乱码的解决方法,本文说下mac下的处理,其实处理方式是一样的,我电脑的mysql版本是5.7.26-log 网上很多帖子都说去/usr/local/mysql/s ...

  3. 如何对Win10电脑文件夹选项进行设置?

    文件夹选项是Windows系统中非常重要的一个功能,在这里能对电脑内的文件及文件夹进行各种各样的设置以及操作.在Windows系统升级到Win10版本后,许多界面都发生了变化,文件夹选项也是如此,打开 ...

  4. deep_learning_Function_sklearn的train_test_split()

    sklearn的train_test_split   train_test_split函数用于将矩阵随机划分为训练子集和测试子集,并返回划分好的训练集测试集样本和训练集测试集标签. 格式: X_tra ...

  5. 【2017-05-04】winfrom进程、线程、用户控件

    一.进程 一个进程就是一个程序,利用进程可以在一个程序中打开另一个程序. 1.开启某个进程Process.Start("文件缩写名"); 注意:Process要解析命名空间. 2. ...

  6. Go语言基础之操作Redis

    Go语言操作Redis 在项目开发中redis的使用也比较频繁,本文介绍了Go语言如何操作Redis. Redis介绍 Redis是一个开源的内存数据库,Redis提供了5种不同类型的数据结构,很多业 ...

  7. 3.Shell 接收用户的参数

    1.Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,Linux系统中的Shell脚本语言已经内设了用于接收参数的变量,变量之间可以使用空格间隔. 例如$0对应的是当前Shel ...

  8. Elasticsearch中Mapping

    映射(Mapping) 概念:创建索引时,可以预先定义字段的类型以及相关属性.从而使得索引建立得更加细致和完善.如果不预先设置映射,会自动识别输入的字段类型. 官方文档(字段数据类型):https:/ ...

  9. 在linux中创建新用户-再次安装python

    原来的阿里云python软件安装错了,用了root安装软件,搞得我后面的软件全部都要用root,软连接也搞不定,卸载也不好卸载.只能格式化,实例什么的都不用重建,系统也不用安装,直接创建用户就行了,磁 ...

  10. 一道经典JS面试题

    超过80%的候选人对下面这道JS面试题的回答情况连及格都达不到.这究竟是怎样神奇的一道JS面试题?他考察了候选人的哪些能力?对正在读本文的你有什么启示? 不起眼的开始 招聘前端工程师,尤其是中高级前端 ...