|
38 | 38 | The range of width and height of the input 2D array is [1,200].
|
39 | 39 | */
|
40 | 40 | public class _533 {
|
41 |
| - /**Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5 |
42 |
| - * |
43 |
| - * This program is very well designed to do things: |
44 |
| - * 1. it scans the entire matrix once, but does two things in this scan: |
45 |
| - * first it records an array of cols that keeps count of how many 'B' are there in each column; |
46 |
| - * second, at the end of traversing each column, it puts this entire row as the key into a HashMap |
47 |
| - * when there N number of 'B's in this row. |
48 |
| - * |
49 |
| - * 2. then we could go through the HashMap keyset: |
50 |
| - * if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's*/ |
51 |
| - public int findBlackPixel(char[][] picture, int N) { |
52 |
| - if (picture == null || picture.length == 0 || picture[0].length == 0) { |
53 |
| - return 0; |
54 |
| - } |
55 |
| - int m = picture.length; |
56 |
| - int n = picture[0].length; |
57 |
| - int[] cols = new int[n]; |
58 |
| - Map<String, Integer> map = new HashMap<>(); |
59 |
| - StringBuilder stringBuilder = new StringBuilder(); |
60 |
| - for (int i = 0; i < m; i++) { |
61 |
| - int count = 0; |
62 |
| - for (int j = 0; j < n; j++) { |
63 |
| - if (picture[i][j] == 'B') { |
64 |
| - count++; |
65 |
| - cols[j]++; |
66 |
| - } |
67 |
| - stringBuilder.append(picture[i][j]); |
| 41 | + public static class Solution1 { |
| 42 | + /** |
| 43 | + * Credit: https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap/5 |
| 44 | + * This program is very well designed to do things: |
| 45 | + * 1. it scans the entire matrix once, but does two things in this scan: |
| 46 | + * first it records an array of cols that keeps count of how many 'B' are there in each column; |
| 47 | + * second, at the end of traversing each column, it puts this entire row as the key into a HashMap |
| 48 | + * when there N number of 'B's in this row. |
| 49 | + * 2. then we could go through the HashMap keyset: |
| 50 | + * if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's |
| 51 | + */ |
| 52 | + public int findBlackPixel(char[][] picture, int N) { |
| 53 | + if (picture == null || picture.length == 0 || picture[0].length == 0) { |
| 54 | + return 0; |
68 | 55 | }
|
69 |
| - if (count == N) { |
70 |
| - /**we use this entire row string as key for the map*/ |
71 |
| - map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1); |
| 56 | + int m = picture.length; |
| 57 | + int n = picture[0].length; |
| 58 | + int[] cols = new int[n]; |
| 59 | + Map<String, Integer> map = new HashMap<>(); |
| 60 | + StringBuilder stringBuilder = new StringBuilder(); |
| 61 | + for (int i = 0; i < m; i++) { |
| 62 | + int count = 0; |
| 63 | + for (int j = 0; j < n; j++) { |
| 64 | + if (picture[i][j] == 'B') { |
| 65 | + count++; |
| 66 | + cols[j]++; |
| 67 | + } |
| 68 | + stringBuilder.append(picture[i][j]); |
| 69 | + } |
| 70 | + if (count == N) { |
| 71 | + /**we use this entire row string as key for the map*/ |
| 72 | + map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1); |
| 73 | + } |
| 74 | + stringBuilder.setLength(0); |
72 | 75 | }
|
73 |
| - stringBuilder.setLength(0); |
74 |
| - } |
75 | 76 |
|
76 |
| - int answer = 0; |
77 |
| - for (String key : map.keySet()) { |
78 |
| - if (map.get(key) != N) { |
79 |
| - continue; |
80 |
| - } |
81 |
| - for (int i = 0; i < n; i++) { |
82 |
| - if (key.charAt(i) == 'B' && cols[i] == N) { |
83 |
| - answer += N; |
| 77 | + int answer = 0; |
| 78 | + for (String key : map.keySet()) { |
| 79 | + if (map.get(key) != N) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + for (int i = 0; i < n; i++) { |
| 83 | + if (key.charAt(i) == 'B' && cols[i] == N) { |
| 84 | + answer += N; |
| 85 | + } |
84 | 86 | }
|
85 | 87 | }
|
| 88 | + return answer; |
86 | 89 | }
|
87 |
| - return answer; |
88 | 90 | }
|
89 | 91 |
|
90 | 92 | }
|
0 commit comments