[Swift] LeetCode1252 odd number of cell values |. Cells with Odd Values in a Matrix

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ personal domain name: https://www.zengqiang.org
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: https://www.cnblogs.com/strengthen/p/11831506.html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.

Return the number of cells with odd values in the matrix after applying the increment to all indices.

Example 1:

Input: n = 2, m = 3, indices = [[0,1],[1,1]]
Output: 6
Explanation: Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.

Example 2:

Input: n = 2, m = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.

Constraints:

  • 1 <= n <= 50
  • 1 <= m <= 50
  • 1 <= indices.length <= 100
  • 0 <= indices[i][0] < n
  • 0 <= indices[i][1] < m

Give you a  n line  m matrix column, the very beginning, the value of each cell is  0.

Otherwise an indexed array  indices, indices[i] = [ri, ci] in  ri and  ci represent the specified row and column (from  0 start number).

You need to make each of  [ri, ci] the specified line and all the cells in the column value added  1.

Please complete the implementation of all  indices the specified increment return matrix "odd Cell Value" number.

Example 1:

Input: n = 2, m = 3 , indices = [[0,1], [1,1]] 
Output: 6 
Explanation: The matrix is the beginning of [[0,0,0], [0,0,0 ]]. 
After the first increment to give [[1,2,1], [0,1,0]]. 
The final matrix is [[1,3,1], [1,3,1]], which has an odd number six.

Example 2:

Input: n = 2, m = 2 , indices = [[1,1], [0,0]] 
Output: 0 
Explanation: The final matrix is [[2,2], [2,2]], there is no odd number.

prompt:

  • 1 <= n <= 50
  • 1 <= m <= 50
  • 1 <= indices.length <= 100
  • 0 <= indices[i][0] < n
  • 0 <= indices[i][1] < m

Runtime: 16 ms
Memory Usage: 20.9 MB
 1 class Solution {
 2     func oddCells(_ n: Int, _ m: Int, _ indices: [[Int]]) -> Int {
 3         var row:[Int] = [Int](repeating: 0, count: n)
 4         var col:[Int] = [Int](repeating: 0, count: m)
 5         for idx in indices
 6         {
 7             row[idx[0]] ^= 1// if row idx[0] appears odd times, it will correspoind to true.
 8             col[idx[1]] ^= 1 // if column idx[1] appears odd times, it will correspoind to true.
 9         }
10         var cnt:Int = 0
11         for i in 0..<n
12         {
13             for j in 0..<m
14             {
15                 cnt += (row[i] ^ col[j]) != 0 ? 1 : 0; // only cell (i, j) with odd times count of row + column would get odd values.
16             }
17         }
18         return cnt
19     }
20 }

 

Guess you like

Origin www.cnblogs.com/strengthen/p/11831506.html