562. Longest Line of Consecutive One in Matrix

This is an easy question.  No matter what kind of method you chose, the time complexity is at least O(m*n), becasue you have to traverse each element at least once. Here, we have a straight forward method without any maps...

class Solution {
public:
    int longestLine(vector<vector<int>>& M) {
        if (M.empty()) return 0;
        int result = 0;
        int cur = 0;
        for (int i = 0; i < M.size(); i++) {
            cur = 0;
            for (int j = 0; j < M[i].size(); j++) {
                if (M[i][j]) cur++;
                else cur = 0;
                result = max(result, cur);
            }
        }
        for (int j = 0; j < M[0].size(); j++) {
            cur = 0;
            for (int i = 0; i < M.size(); i++) {
                if (M[i][j]) cur++;
                else cur = 0;
                result = max(result, cur);
            }
        }

        //int my = M.size(), mx = M[0].size();
        for (int j = -(int)M.size(); j < (int)M[0].size(); j++) {
            cur = 0;
            for (int i = 0; i < M.size(); i++) {
                if (j + i < 0 || i + j >= M[0].size()) continue;
                else {
                    if (M[i][j + i]) cur++;
                    else cur = 0;
                    result = max(result, cur);
                }
            }
        }
        for (int j = 0; j < M[0].size() + M.size(); j++) {
            cur = 0;
            for (int i = 0; i < M.size(); i++) {
                if (j - i < 0 || j - i >= M[0].size()) continue;
                else {
                    if (M[i][j - i]) cur++;
                    else cur = 0;
                    result = max(result, cur);
                }
            }
        }
        return result;
    }
};

Pay attention to the third for-loop. Because the return value of size() is size_type, an unsigned value, when you make some operations with int, be very careful about bugs, especially when the value is minus.

时间: 2024-07-30 10:03:12

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

[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 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 matr

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

Longest Continuance Increasing sub-sequence in matrix

一个矩阵,求最长连续的序列长度 [1 2 3 4 5 6 7 8 9] 我的解法时用dfs遍历矩阵,如果便利过的元素就标记为false,不再遍历. 邻居 就是上下左右 e.g. 1 2 3 6 5 4 -> 7 7 9 8 =================== 5 7 9 1 2 3 -> 3 4 6 8 =================== 9 8 7 4 5 6 ->9 3 2 1 题目一. 只能上下左右,走到边界处止步 题目二. 每一行/列都是一个循环,走到边界处譬如第 0 列

Longest Increasing Continuous subsequence II

Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence in this matrix. (The definition of the longest increasing continuous subsequence here can start at any row or column and go up/down/right/l

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

Longest Continuous Increasing Subsequence II

Description Given an integer matrix. Find the longest increasing continuous subsequence in this matrix and return the length of it. The longest increasing continuous subsequence here can start at any position and go up/down/left/right. Example Exampl

HDU 3911 Black And White(线段树区间合并)

Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she wan