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 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的更多相关文章
- 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 ...
- 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 ...
- LC 516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LC 3. Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LC 967. Numbers With Same Consecutive Differences
Return all non-negative integers of length N such that the absolute difference between every two con ...
- 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 ...
- [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 ...
- [LC] 1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predece ...
随机推荐
- Linux:rm可不可以实现删除所有文件,除了demo文件
方法1: shopt -s extglob #开启扩展通配符 rm -rf !(demo) #删除除了demo的文件 方法2: find /test -not -name "demo&quo ...
- Mount Windows (CIFS) shares on Linux with credentials in a secure way
Posted on 09/09/2014 In almost all cases, when mounting a CIFS-share on a Linux host, you will nee ...
- SQL语句复习【专题四】
SQL语句复习[专题四] 多表查询 sql 92多表查询 sql92.sql99 标准--查询所有员工的姓名,部门编号,部门名称select * from empselect * from dept- ...
- c3p0的错误mchange.v2.ser.Indirector
mchange-commons-java-0.2.11.jar 没有会报错 (java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirec ...
- HashSet与HashMap源代码深度剖析
HashSet源码分析: 先来看一下它的构造方法: 呃~~居然它的底层是用HashMap来实现的,颠覆三观,那它究竟是如何来用的呢?继续来往下跟: 对于HashSet而言是没有key->valu ...
- selenium 实战
iframe driver.switch_to_frame(id="xx") 多窗口 from selenium import webdriver from time import ...
- pycharm 快捷键及一些常用设置
pycharm中的快捷键及一些常用设置 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到ReferenceCard.pdf快捷键英文版说明 PyCharm Default ...
- git fetch, merge, pull, push需要注意的地方
在git操作中,我们经常会用到fetch, merge, pull和push等命令,以下是一些我们需要注意的地方. 给大家准备了参考资料: 1. Whatʼs a Fast Forward Merge ...
- 第八章 watch监听 84 watch-监视路由地址的改变
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 磁盘阵列(RAID)
RAID 0亦称为带区集.它将两个以上的磁盘并联起来,成为一个大容量的磁盘.在存放数据时,分段后分散存储在这些磁盘中,因为读写时都可以并行处理,所以在所有的级别中,RAID 0的速度是最快的.但是RA ...