Python straightforward

  • 0
    class Solution(object):
        def longestLine(self, M):
            import numpy as np
            res = []
            def longest_ones(a):
                l = a.tolist()
                s = ''.join(map(str, l))
                ones = re.findall('1+', s)
                return max(map(len, ones)) if ones else 0
            a = np.array(M)
            shape = a.shape
            res.extend([a[i,:] for i in range(shape[0])])
            if len(shape) == 2:
                res.extend([a[:,i] for i in range(shape[1])])
                res.extend([a.diagonal(i) for i in range(-shape[0]+1, shape[1])])
                res.extend([np.fliplr(a).diagonal(i) for i in range(-shape[0]+1, shape[1])])
            ones = map(longest_ones, res)
            return max(ones) if ones else 0

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.