Leetcode1252. Cells with Odd Values in a Matrix

int oddCells public (n-int, int m, int [] [] indices) { 
int [] [] the Array = new new int [n-] [m];
int = rowlength indices.length; // Number of rows array indices obtained
for (int i = 0; i < rowlength; i ++) {// begin separately for each index is increasing ranks
for (int j = 0; j <m; j ++) {// add the first row
Array [indices [i ] [0]] [J] + =. 1;
}
for (int K = 0; K <n-; K ++) {// then the column was added
Array [k] [indices [i ] [1]] + = 1;
}
}
// statistics inside the even number
int ODD = 0;
for (int I = 0; I <n-; I ++) {
for (int J = 0; J <m; J ++)
IF (the Array [I] [J] == 2. 1%)
ODD ++;
}
System.out.println (ODD);
return ODD;
}
method of low complexity time

 

 Solutions:

Later found the law

In fact, the fundamental elements of the matrix not obtained

The only line conversion and frequency transformation columns, odd and even may be obtained

public int oddCells(int n, int m, int[][] indices) {
int[] row = new int[n];
int[] col = new int[m];
for (int i = 0; i < indices.length; i++) {
row[indices[i][0]] += 1;
col[indices[i][1]] += 1;
}
int odd = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if ((row[i] + col[j]) % 2 == 1)
odd++;
}
System.out.println(odd);
return odd;
}

Guess you like

Origin www.cnblogs.com/chengxian/p/12204030.html