Skip to content

Commit 4873ddf

Browse files
committed
https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/
1 parent c14d795 commit 4873ddf

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ https://leetcode.cn/problems/replace-words/
890890

891891
https://leetcode.cn/problems/count-servers-that-communicate/
892892

893+
https://leetcode.cn/problems/cells-with-odd-values-in-a-matrix/
894+
893895
#### 安装教程
894896

895897
1. 安装`deno`

calculator-lcci/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals } from "../deps.ts";
22
import calculate from "./index.ts";
3-
Deno.test("testcase-calculator", () => {
3+
Deno.test("calculator-lcci", () => {
44
assertEquals(calculate("3+2*2"), 7);
55
assertEquals(calculate("3/2"), 1);
66
assertEquals(calculate(" 3+5 / 2 "), 5);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function binary_count_one_bigint(n: bigint) {
2+
let ans = 0n;
3+
while (n !== 0n) {
4+
if (n % 2n) {
5+
ans++;
6+
}
7+
n >>= 1n;
8+
}
9+
return ans;
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { binary_count_one_bigint } from "./binary_count_one_bigint.ts";
2+
3+
function oddCells(m: number, n: number, indices: number[][]): number {
4+
let row = 0n;
5+
let col = 0n;
6+
for (const [r, c] of indices) {
7+
row ^= 1n << BigInt(r);
8+
col ^= 1n << BigInt(c);
9+
}
10+
11+
const x = Number(binary_count_one_bigint(row));
12+
const y = Number(binary_count_one_bigint(col));
13+
return x * n + y * m - 2 * x * y;
14+
}
15+
export default oddCells;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { assertEquals } from "../deps.ts";
2+
import oddCells from "./index.ts";
3+
4+
Deno.test("cells-with-odd-values-in-a-matrix", () => {
5+
assertEquals(
6+
6,
7+
oddCells(2, 3, [
8+
[0, 1],
9+
[1, 1],
10+
]),
11+
);
12+
assertEquals(
13+
0,
14+
oddCells(2, 2, [
15+
[1, 1],
16+
[0, 0],
17+
]),
18+
);
19+
assertEquals(
20+
659,
21+
oddCells(41, 38, [
22+
[38, 33],
23+
[37, 24],
24+
[33, 6],
25+
[15, 0],
26+
[13, 22],
27+
[1, 13],
28+
[12, 8],
29+
[20, 4],
30+
[30, 9],
31+
[21, 0],
32+
[35, 29],
33+
[21, 20],
34+
[36, 24],
35+
[40, 16],
36+
[3, 24],
37+
[32, 6],
38+
[19, 28],
39+
[1, 13],
40+
[30, 19],
41+
[28, 20],
42+
[28, 2],
43+
]),
44+
);
45+
});

mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { inorder } from "./binary-tree-inorder-traversal/inorder.ts";
6363
import { buildBST } from "./convert-sorted-list-to-binary-search-tree/buildBST.ts";
6464
import { traversal_bst_range } from "./tweet-counts-per-frequency/traversal_bst_range.ts";
6565
import { left_rotate } from "./spiral-matrix/left_rotate.ts";
66+
import { binary_count_one_bigint } from "./cells-with-odd-values-in-a-matrix/binary_count_one_bigint.ts";
6667
export {
6768
left_rotate,
6869
TrieNode,
@@ -103,6 +104,6 @@ export { abs_bigint, searchSegmentChildren, searchSegmentLeaf };
103104
export { counter };
104105
export { Node as MultilevelDoublyLinkedListNode } from "./flatten-a-multilevel-doubly-linked-list/Node.ts";
105106
export { MyLinkedList };
106-
export { level };
107+
export { binary_count_one_bigint, level };
107108
export { buildBST, inorder };
108109
export { traversal_bst_range };

0 commit comments

Comments
 (0)