Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions solution/0600-0699/0645.Set Mismatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,29 +295,29 @@ function findErrorNums(nums: number[]): number[] {
#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
let n = nums.len() as i32;
let mut cnt: HashMap<i32, i32> = HashMap::new();

for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];

let mut ans = vec![0; 2];

for x in 1..=n {
let c = *cnt.get(&x).unwrap_or(&0);
if c == 2 {
ans[0] = x;
} else if c == 0 {
ans[1] = x;
}
}
vec![b, a]

ans
}
}
```
Expand Down Expand Up @@ -477,6 +477,36 @@ function findErrorNums(nums: number[]): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];
}
}
vec![b, a]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
66 changes: 48 additions & 18 deletions solution/0600-0699/0645.Set Mismatch/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,29 +286,29 @@ function findErrorNums(nums: number[]): number[] {
#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
let n = nums.len() as i32;
let mut cnt: HashMap<i32, i32> = HashMap::new();

for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];

let mut ans = vec![0; 2];

for x in 1..=n {
let c = *cnt.get(&x).unwrap_or(&0);
if c == 2 {
ans[0] = x;
} else if c == 0 {
ans[1] = x;
}
}
vec![b, a]

ans
}
}
```
Expand Down Expand Up @@ -468,6 +468,36 @@ function findErrorNums(nums: number[]): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];
}
}
vec![b, a]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
36 changes: 18 additions & 18 deletions solution/0600-0699/0645.Set Mismatch/Solution2.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use std::collections::HashMap;

impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
let n = nums.len() as i32;
let mut cnt: HashMap<i32, i32> = HashMap::new();

for &x in &nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];

let mut ans = vec![0; 2];

for x in 1..=n {
let c = *cnt.get(&x).unwrap_or(&0);
if c == 2 {
ans[0] = x;
} else if c == 0 {
ans[1] = x;
}
}
vec![b, a]

ans
}
}
25 changes: 25 additions & 0 deletions solution/0600-0699/0645.Set Mismatch/Solution3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
impl Solution {
pub fn find_error_nums(nums: Vec<i32>) -> Vec<i32> {
let mut xs = 0;
for (i, x) in nums.iter().enumerate() {
xs ^= ((i + 1) as i32) ^ x;
}
let mut a = 0;
let lb = xs & -xs;
for (i, x) in nums.iter().enumerate() {
if (((i + 1) as i32) & lb) != 0 {
a ^= (i + 1) as i32;
}
if (*x & lb) != 0 {
a ^= *x;
}
}
let b = xs ^ a;
for x in nums.iter() {
if *x == a {
return vec![a, b];
}
}
vec![b, a]
}
}
130 changes: 128 additions & 2 deletions solution/0600-0699/0647.Palindromic Substrings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ tags:

### 方法一:从中心向两侧扩展回文串

时间复杂度 $O(n^2)$,其中 $n$ 是字符串 `s` 的长度。
我们可以枚举回文串的中心位置,然后向两侧扩展,统计回文串的数量。对于长度为 $n$ 的字符串,回文串的中心位置共有 $2n-1$ 个(包括奇数长度和偶数长度的回文串)。对于每个中心位置,我们向两侧扩展,直到不满足回文串的条件为止,统计回文串的数量。

时间复杂度 $O(n^2)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -166,7 +168,7 @@ var countSubstrings = function (s) {

在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$。
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的“最大回文长度”表述容易引起歧义:本实现里 p[i] 是在插入分隔符后的变换字符串上的回文半径(radius),不等同于原字符串中的长度。建议注明这是变换字符串上的半径/扩展长度,并说明与代码里 ans += p[i] / 2(或 floor(p[i]/2))的对应关系,避免读者误解。

Suggested change
在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$
在 Manacher 算法中,我们先构造插入分隔符后的变换字符串 `t`,此时 `p[i]` 表示在字符串 `t` 上以第 `i` 位为中心的回文半径(radius,至少为 1,单位是「变换串中的字符数」)。对应到原字符串上,以第 `i` 位为中心所能产生的回文子串数量正好是 `p[i] // 2`,这也是代码中 `ans += p[i] // 2` 的含义

Copilot uses AI. Check for mistakes.

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 `s` 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -221,6 +223,130 @@ class Solution {
}
```

#### C++

```cpp
class Solution {
public:
int countSubstrings(string s) {
string t = "^#";
for (char c : s) {
t += c;
t += '#';
}
t += "$";

int n = t.size();
vector<int> p(n, 0);
int pos = 0, maxRight = 0;
int ans = 0;

for (int i = 1; i < n - 1; ++i) {
if (maxRight > i) {
p[i] = min(maxRight - i, p[2 * pos - i]);
} else {
p[i] = 1;
}

while (t[i - p[i]] == t[i + p[i]]) {
++p[i];
}

if (i + p[i] > maxRight) {
maxRight = i + p[i];
pos = i;
}

ans += p[i] / 2;
}

return ans;
}
};
```

#### Go

```go
func countSubstrings(s string) int {
t := "^#"
for _, c := range s {
t += string(c)
t += "#"
}
t += "$"

n := len(t)
p := make([]int, n)
pos, maxRight := 0, 0
ans := 0

for i := 1; i < n-1; i++ {
if maxRight > i {
mirror := 2*pos - i
if p[mirror] < maxRight-i {
p[i] = p[mirror]
} else {
p[i] = maxRight - i
}
} else {
p[i] = 1
}

for t[i-p[i]] == t[i+p[i]] {
p[i]++
}

if i+p[i] > maxRight {
maxRight = i + p[i]
pos = i
}

ans += p[i] / 2
}

return ans
}
```

#### TypeScript

```ts
function countSubstrings(s: string): number {
let t = "^#";
for (const c of s) {
t += c + "#";
}
t += "$";

const n = t.length;
const p: number[] = new Array(n).fill(0);
let pos = 0, maxRight = 0;
let ans = 0;

for (let i = 1; i < n - 1; i++) {
if (maxRight > i) {
p[i] = Math.min(maxRight - i, p[2 * pos - i]);
} else {
p[i] = 1;
}

while (t[i - p[i]] === t[i + p[i]]) {
p[i]++;
}

if (i + p[i] > maxRight) {
maxRight = i + p[i];
pos = i;
}

ans += Math.floor(p[i] / 2);
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading