Simple Idea Java Solution


  • 0
    public class Solution {
        private int m, n;
        public int findLonelyPixel(char[][] matrix) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
            m = matrix.length;
            n = matrix[0].length;
            Set<Integer> rows = new HashSet<>();
            Set<Integer> cols = new HashSet<>();
            int res = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    boolean flag = true;
                    if (matrix[i][j] == 'B' && rows.add(i) && cols.add(j)) {
                        for (int k = i + 1; k < m; k++) {
                            if (matrix[k][j] == 'B') flag = false;
                        }
                        for (int k = i - 1; k >= 0; k--) {
                            if (matrix[k][j] == 'B') flag = false;
                        }
                        for (int k = j + 1; k < n; k++) {
                            if (matrix[i][k] == 'B') flag = false;
                        }
                        for (int k = j - 1; k >= 0 ; k--) {
                            if (matrix[i][k] == 'B') flag = false;
                        }
                        if (flag) res++;
                    }
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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