simple java solution 20ms 100%beat


  • 0
    T
        public int findLonelyPixel(char[][] picture) {
            if (picture.length == 0) {
                return 0;
            }
            int res = 0;
            int x = picture.length;
            int y = picture[0].length;
            boolean[][] tag = new boolean[x][y];
            for (int i = 0; i < x; i ++) {
                for (int j = 0; j < y; j ++) {
                    if (picture[i][j] == 'W') {
                        continue;
                    }
                    if (!tag[i][j] && scanRow(i,j ,picture, tag) && scanColumn(i, j ,picture, tag)) {
                        res += 1;
                    }
                    break;
                }
            }
            return res;
        }
        
        private boolean scanRow(int i, int j, char[][] A, boolean[][] B) {
            int k = j + 1;
            boolean tag = true;
            while (k < A[i].length) {
                if (A[i][k] == 'B') {
                    B[i][k] = true;
                    tag = false;
                }
                k ++;
            }
            return tag;
        }
         private boolean scanColumn(int i, int j, char[][] A, boolean[][] B) {
            int k = i + 1;
            boolean tag = true;
            while (k < A.length) {
                if (A[k][j] == 'B') {
                    B[k][j] = true;
                    tag = false;
                }
                k ++;
            }
            return tag;
        }
    

Log in to reply
 

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