OMG...My ugly solution


  • 0
    public class Solution {
        private int m, n;
        private int max = 0;
        public int longestLine(int[][] M) {
            if (M == null || M.length == 0 || M[0].length == 0) return 0;
            m = M.length;
            n = M[0].length;
            horizontal(M);
            vertical(M);
            diagonal(M);
            antiDiagonal(M);
            return max;
        }
        private void horizontal(int[][] M) {
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    if (M[i][j] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }
                }
            }
        }
        private void vertical(int[][] M) {
            for (int j = 0; j < n; j++) {
                int temp = 0;
                for (int i = 0; i < m; i++) {
                    if (M[i][j] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }
                }
            }
        }
        private void diagonal(int[][] M) {
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int row = i, col = 0; row < m && col < n; row++, col++) {
                    if (M[row][col] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }
                }
            }
            for (int j = 1; j < n; j++) {
                int temp = 0;
                for (int row = m - 1, col = j; row >= 0 && col < n; row--, col++) {
                    if (M[row][col] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }                
                }
            }
        }
        private void antiDiagonal(int[][] M) { // start from the left-bottom
            for (int i = m - 1; i >= 0; i--) {
                int temp = 0;
                for (int row = i, col = 0; row >= 0 && col < n; row--, col++) {
                    if (M[row][col] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }
                }
            }
            for (int j = 1; j < n; j++) {
                int temp = 0;
                for (int row = 0, col = j; row < m && col < n; row++, col++) {
                    if (M[row][col] == 1) {
                        temp++;
                        max = Math.max(max, temp);
                    } else {
                        temp = 0;
                    }
                }
            }
        }
    }
    

Log in to reply
 

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