From bc78ae8d59c772d423ceab21dcb11529d2150f0e Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 27 Oct 2025 07:09:26 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.2128,2130 --- .../README.md | 23 ++++++++++ .../README_EN.md | 45 ++++++++++++++++++- .../Solution.rs | 18 ++++++++ .../README.md | 6 ++- .../README_EN.md | 10 +++-- 5 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 solution/2100-2199/2128.Remove All Ones With Row and Column Flips/Solution.rs diff --git a/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README.md b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README.md index 373fbed95ad97..3e9e84180087a 100644 --- a/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README.md +++ b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README.md @@ -178,6 +178,29 @@ function removeOnes(grid: number[][]): boolean { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn remove_ones(grid: Vec>) -> bool { + let n = grid[0].len(); + let mut set = HashSet::new(); + + for row in grid.iter() { + let mut pattern = String::with_capacity(n); + for &x in row.iter() { + pattern.push(((row[0] ^ x) as u8 + b'0') as char); + } + set.insert(pattern); + } + + set.len() == 1 + } +} +``` + diff --git a/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README_EN.md b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README_EN.md index 3eb12f5804665..a3c12fa2cdd5d 100644 --- a/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README_EN.md +++ b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README_EN.md @@ -68,7 +68,27 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +We observe that if two rows in the matrix satisfy one of the following conditions, they can be made equal by flipping certain columns: + +1. The corresponding elements of the two rows are equal, i.e., if one row is $1,0,0,1$, the other row is also $1,0,0,1$; +1. The corresponding elements of the two rows are opposite, i.e., if one row is $1,0,0,1$, the other row is $0,1,1,0$. + +We call two rows that satisfy one of the above conditions "equivalent rows." The answer to the problem is the maximum number of equivalent rows in the matrix. + +Therefore, we can traverse each row of the matrix and convert each row into an "equivalent row" that starts with $0$. Specifically: + +- If the first element of the current row is $0$, keep the row unchanged; +- If the first element of the current row is $1$, flip every element in the row, i.e., $0$ becomes $1$, $1$ becomes $0$. In other words, we flip rows starting with $1$ into "equivalent rows" starting with $0$. + +In this way, we only need to use a hash table to count each converted row. If the hash table contains only one element at the end, it means we can remove all $1$s from the matrix by flipping rows or columns. + +The time complexity is $O(mn)$ and the space complexity is $O(m)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. + +Related problem: + +- [1072. Flip Columns For Maximum Number of Equal Rows](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1072.Flip%20Columns%20For%20Maximum%20Number%20of%20Equal%20Rows/README_EN.md) @@ -159,6 +179,29 @@ function removeOnes(grid: number[][]): boolean { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn remove_ones(grid: Vec>) -> bool { + let n = grid[0].len(); + let mut set = HashSet::new(); + + for row in grid.iter() { + let mut pattern = String::with_capacity(n); + for &x in row.iter() { + pattern.push(((row[0] ^ x) as u8 + b'0') as char); + } + set.insert(pattern); + } + + set.len() == 1 + } +} +``` + diff --git a/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/Solution.rs b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/Solution.rs new file mode 100644 index 0000000000000..518b8d0146221 --- /dev/null +++ b/solution/2100-2199/2128.Remove All Ones With Row and Column Flips/Solution.rs @@ -0,0 +1,18 @@ +use std::collections::HashSet; + +impl Solution { + pub fn remove_ones(grid: Vec>) -> bool { + let n = grid[0].len(); + let mut set = HashSet::new(); + + for row in grid.iter() { + let mut pattern = String::with_capacity(n); + for &x in row.iter() { + pattern.push(((row[0] ^ x) as u8 + b'0') as char); + } + set.insert(pattern); + } + + set.len() == 1 + } +} diff --git a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md index 89f9b62d1dee6..0998043e253c6 100644 --- a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md +++ b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md @@ -82,9 +82,11 @@ tags: -### 方法一:链表转成列表(数组)求解 +### 方法一:模拟 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 +我们可以将链表中的节点值依次存入数组中,然后使用双指针分别指向数组的开头和结尾,计算每对孪生节点的孪生和,取最大值即为答案。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表的节点数。 diff --git a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md index 689cc69226e20..1db44b0c2013a 100644 --- a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md +++ b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md @@ -39,7 +39,7 @@ tags: Explanation: Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6. There are no other nodes with twins in the linked list. -Thus, the maximum twin sum of the linked list is 6. +Thus, the maximum twin sum of the linked list is 6.

Example 2:

@@ -51,7 +51,7 @@ Thus, the maximum twin sum of the linked list is 6. The nodes with twins present in this linked list are: - Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7. - Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4. -Thus, the maximum twin sum of the linked list is max(7, 4) = 7. +Thus, the maximum twin sum of the linked list is max(7, 4) = 7.

Example 3:

@@ -77,7 +77,11 @@ There is only one node with a twin in the linked list having twin sum of 1 + 100 -### Solution 1 +### Solution 1: Simulation + +We can store the values of the nodes in the linked list into an array, then use two pointers pointing to the beginning and end of the array to calculate the twin sum for each pair of twin nodes. The maximum twin sum is the answer. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in the linked list.