class Solution {
// 使用异或
public int maxEqualRowsAfterFlips(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int res = 0;
Map<String, Integer> map = new HashMap<>();
int m = matrix.length;
int n = matrix[0].length;
boolean firstZero = false;
for (int i = 0; i < m; i++){
if (matrix[i][0] == 0){
firstZero = true;
} else {
firstZero = false;
}
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++ ){
if (firstZero){
sb.append(matrix[i][j]);
} else{
sb.append(matrix[i][j] ^ 1);
}
}
String temp = sb.toString();
map.put(temp, map.getOrDefault(temp, 0) + 1);
res = Math.max(res, map.get(temp));
}
return res;
}
}
力扣:1072. 按列翻转得到最大值等行数
最新推荐文章于 2024-08-15 05:02:54 发布