在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given Return the number of cells with odd values in the matrix after applying the increment to all 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 = 2, m = 3, indices = [[0,1],[1,1]] 输出:6 解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。 第一次增量操作后得到 [[1,2,1],[0,1,0]]。 最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。 示例 2: 输入:n = 2, m = 2, indices = [[1,1],[0,0]] 输出:0 解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。 提示:
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 }
|
请发表评论