mirror of https://github.com/doocs/leetcode.git
feat: update lc problems (#3635)
This commit is contained in:
parent
6ae17e2ca1
commit
0ec4c29cce
|
|
@ -103,34 +103,6 @@ func addDigits(num int) int {
|
|||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn add_digits(num: i32) -> i32 {
|
||||
if num < 10 {
|
||||
return num;
|
||||
}
|
||||
Self::add_digits(
|
||||
num.to_string()
|
||||
.chars()
|
||||
.map(|c| c.to_string().parse::<i32>().unwrap())
|
||||
.sum::<i32>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### 方法二
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn add_digits(mut num: i32) -> i32 {
|
||||
|
|
|
|||
|
|
@ -101,34 +101,6 @@ func addDigits(num int) int {
|
|||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn add_digits(num: i32) -> i32 {
|
||||
if num < 10 {
|
||||
return num;
|
||||
}
|
||||
Self::add_digits(
|
||||
num.to_string()
|
||||
.chars()
|
||||
.map(|c| c.to_string().parse::<i32>().unwrap())
|
||||
.sum::<i32>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 2
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Rust
|
||||
|
||||
```rust
|
||||
impl Solution {
|
||||
pub fn add_digits(mut num: i32) -> i32 {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,5 @@
|
|||
impl Solution {
|
||||
pub fn add_digits(num: i32) -> i32 {
|
||||
if num < 10 {
|
||||
return num;
|
||||
}
|
||||
Self::add_digits(
|
||||
num.to_string()
|
||||
.chars()
|
||||
.map(|c| c.to_string().parse::<i32>().unwrap())
|
||||
.sum::<i32>(),
|
||||
)
|
||||
pub fn add_digits(mut num: i32) -> i32 {
|
||||
((num - 1) % 9) + 1
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
impl Solution {
|
||||
pub fn add_digits(mut num: i32) -> i32 {
|
||||
((num - 1) % 9) + 1
|
||||
}
|
||||
}
|
||||
|
|
@ -62,7 +62,18 @@ tags:
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### 方法一
|
||||
### 方法一:排序 + 双指针 + 枚举
|
||||
|
||||
由于元素的顺序不影响结果,我们可以先对数组进行排序,然后使用双指针的方法来解决这个问题。
|
||||
|
||||
我们先将数组排序,然后枚举第一个元素 $\textit{nums}[i]$,并在 $\textit{nums}[i+1:n-1]$ 的区间内使用双指针分别指向 $\textit{nums}[j]$ 和 $\textit{nums}[k]$,其中 $j$ 是 $\textit{nums}[i]$ 的下一个元素,而 $k$ 是数组的最后一个元素。
|
||||
|
||||
- 如果 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] < \textit{target}$,那么对于任意 $j \lt k' \leq k$ 的元素,都有 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k'] \lt \textit{target}$,一共有 $k - j$ 个这样的 $k'$,我们将 $k - j$ 累加到答案中。接下来,将 $j$ 右移一个位置,继续寻找下一个满足条件的 $k$,直到 $j \geq k$ 为止。
|
||||
- 如果 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] \geq \textit{target}$,那么对于任意 $j \leq j' \lt k$ 的元素,都不可能使得 $\textit{nums}[i] + \textit{nums}[j'] + \textit{nums}[k] \lt \textit{target}$,因此我们将 $k$ 左移一个位置,继续寻找下一个满足条件的 $k$,直到 $j \geq k$ 为止。
|
||||
|
||||
枚举完所有的 $i$ 后,我们就得到了满足条件的三元组的个数。
|
||||
|
||||
时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -73,15 +84,15 @@ class Solution:
|
|||
def threeSumSmaller(self, nums: List[int], target: int) -> int:
|
||||
nums.sort()
|
||||
ans, n = 0, len(nums)
|
||||
for i in range(n):
|
||||
for i in range(n - 2):
|
||||
j, k = i + 1, n - 1
|
||||
while j < k:
|
||||
s = nums[i] + nums[j] + nums[k]
|
||||
if s >= target:
|
||||
k -= 1
|
||||
else:
|
||||
x = nums[i] + nums[j] + nums[k]
|
||||
if x < target:
|
||||
ans += k - j
|
||||
j += 1
|
||||
else:
|
||||
k -= 1
|
||||
return ans
|
||||
```
|
||||
|
||||
|
|
@ -91,17 +102,16 @@ class Solution:
|
|||
class Solution {
|
||||
public int threeSumSmaller(int[] nums, int target) {
|
||||
Arrays.sort(nums);
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.length; i < n; ++i) {
|
||||
int j = i + 1;
|
||||
int k = n - 1;
|
||||
int ans = 0, n = nums.length;
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -116,17 +126,17 @@ class Solution {
|
|||
class Solution {
|
||||
public:
|
||||
int threeSumSmaller(vector<int>& nums, int target) {
|
||||
sort(nums.begin(), nums.end());
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.size(); i < n; ++i) {
|
||||
ranges::sort(nums);
|
||||
int ans = 0, n = nums.size();
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target)
|
||||
--k;
|
||||
else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -138,22 +148,45 @@ public:
|
|||
#### Go
|
||||
|
||||
```go
|
||||
func threeSumSmaller(nums []int, target int) int {
|
||||
func threeSumSmaller(nums []int, target int) (ans int) {
|
||||
sort.Ints(nums)
|
||||
ans := 0
|
||||
for i, n := 0, len(nums); i < n; i++ {
|
||||
n := len(nums)
|
||||
for i := 0; i < n-2; i++ {
|
||||
j, k := i+1, n-1
|
||||
for j < k {
|
||||
s := nums[i] + nums[j] + nums[k]
|
||||
if s >= target {
|
||||
k--
|
||||
} else {
|
||||
x := nums[i] + nums[j] + nums[k]
|
||||
if x < target {
|
||||
ans += k - j
|
||||
j++
|
||||
} else {
|
||||
k--
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
#### TypeScript
|
||||
|
||||
```ts
|
||||
function threeSumSmaller(nums: number[], target: number): number {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -167,17 +200,17 @@ func threeSumSmaller(nums []int, target int) int {
|
|||
*/
|
||||
var threeSumSmaller = function (nums, target) {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0, n = nums.length; i < n; ++i) {
|
||||
let j = i + 1;
|
||||
let k = n - 1;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,18 @@ tags:
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 1
|
||||
### Solution 1: Sorting + Two Pointers + Enumeration
|
||||
|
||||
Since the order of elements does not affect the result, we can sort the array first and then use the two-pointer method to solve this problem.
|
||||
|
||||
First, we sort the array and then enumerate the first element $\textit{nums}[i]$. Within the range $\textit{nums}[i+1:n-1]$, we use two pointers pointing to $\textit{nums}[j]$ and $\textit{nums}[k]$, where $j$ is the next element of $\textit{nums}[i]$ and $k$ is the last element of the array.
|
||||
|
||||
- If $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] < \textit{target}$, then for any element $j \lt k' \leq k$, we have $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k'] < \textit{target}$. There are $k - j$ such $k'$, and we add $k - j$ to the answer. Next, move $j$ one position to the right and continue to find the next $k$ that meets the condition until $j \geq k$.
|
||||
- If $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] \geq \textit{target}$, then for any element $j \leq j' \lt k$, it is impossible to make $\textit{nums}[i] + \textit{nums}[j'] + \textit{nums}[k] < \textit{target}$. Therefore, we move $k$ one position to the left and continue to find the next $k$ that meets the condition until $j \geq k$.
|
||||
|
||||
After enumerating all $i$, we get the number of triplets that meet the condition.
|
||||
|
||||
The time complexity is $O(n^2)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -72,15 +83,15 @@ class Solution:
|
|||
def threeSumSmaller(self, nums: List[int], target: int) -> int:
|
||||
nums.sort()
|
||||
ans, n = 0, len(nums)
|
||||
for i in range(n):
|
||||
for i in range(n - 2):
|
||||
j, k = i + 1, n - 1
|
||||
while j < k:
|
||||
s = nums[i] + nums[j] + nums[k]
|
||||
if s >= target:
|
||||
k -= 1
|
||||
else:
|
||||
x = nums[i] + nums[j] + nums[k]
|
||||
if x < target:
|
||||
ans += k - j
|
||||
j += 1
|
||||
else:
|
||||
k -= 1
|
||||
return ans
|
||||
```
|
||||
|
||||
|
|
@ -90,17 +101,16 @@ class Solution:
|
|||
class Solution {
|
||||
public int threeSumSmaller(int[] nums, int target) {
|
||||
Arrays.sort(nums);
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.length; i < n; ++i) {
|
||||
int j = i + 1;
|
||||
int k = n - 1;
|
||||
int ans = 0, n = nums.length;
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -115,17 +125,17 @@ class Solution {
|
|||
class Solution {
|
||||
public:
|
||||
int threeSumSmaller(vector<int>& nums, int target) {
|
||||
sort(nums.begin(), nums.end());
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.size(); i < n; ++i) {
|
||||
ranges::sort(nums);
|
||||
int ans = 0, n = nums.size();
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target)
|
||||
--k;
|
||||
else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -137,22 +147,45 @@ public:
|
|||
#### Go
|
||||
|
||||
```go
|
||||
func threeSumSmaller(nums []int, target int) int {
|
||||
func threeSumSmaller(nums []int, target int) (ans int) {
|
||||
sort.Ints(nums)
|
||||
ans := 0
|
||||
for i, n := 0, len(nums); i < n; i++ {
|
||||
n := len(nums)
|
||||
for i := 0; i < n-2; i++ {
|
||||
j, k := i+1, n-1
|
||||
for j < k {
|
||||
s := nums[i] + nums[j] + nums[k]
|
||||
if s >= target {
|
||||
k--
|
||||
} else {
|
||||
x := nums[i] + nums[j] + nums[k]
|
||||
if x < target {
|
||||
ans += k - j
|
||||
j++
|
||||
} else {
|
||||
k--
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
#### TypeScript
|
||||
|
||||
```ts
|
||||
function threeSumSmaller(nums: number[], target: number): number {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -166,17 +199,17 @@ func threeSumSmaller(nums []int, target int) int {
|
|||
*/
|
||||
var threeSumSmaller = function (nums, target) {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0, n = nums.length; i < n; ++i) {
|
||||
let j = i + 1;
|
||||
let k = n - 1;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
class Solution {
|
||||
public:
|
||||
int threeSumSmaller(vector<int>& nums, int target) {
|
||||
sort(nums.begin(), nums.end());
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.size(); i < n; ++i) {
|
||||
ranges::sort(nums);
|
||||
int ans = 0, n = nums.size();
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target)
|
||||
--k;
|
||||
else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
func threeSumSmaller(nums []int, target int) int {
|
||||
func threeSumSmaller(nums []int, target int) (ans int) {
|
||||
sort.Ints(nums)
|
||||
ans := 0
|
||||
for i, n := 0, len(nums); i < n; i++ {
|
||||
n := len(nums)
|
||||
for i := 0; i < n-2; i++ {
|
||||
j, k := i+1, n-1
|
||||
for j < k {
|
||||
s := nums[i] + nums[j] + nums[k]
|
||||
if s >= target {
|
||||
k--
|
||||
} else {
|
||||
x := nums[i] + nums[j] + nums[k]
|
||||
if x < target {
|
||||
ans += k - j
|
||||
j++
|
||||
} else {
|
||||
k--
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
class Solution {
|
||||
public int threeSumSmaller(int[] nums, int target) {
|
||||
Arrays.sort(nums);
|
||||
int ans = 0;
|
||||
for (int i = 0, n = nums.length; i < n; ++i) {
|
||||
int j = i + 1;
|
||||
int k = n - 1;
|
||||
int ans = 0, n = nums.length;
|
||||
for (int i = 0; i + 2 < n; ++i) {
|
||||
int j = i + 1, k = n - 1;
|
||||
while (j < k) {
|
||||
int s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
int x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,17 +5,17 @@
|
|||
*/
|
||||
var threeSumSmaller = function (nums, target) {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0, n = nums.length; i < n; ++i) {
|
||||
let j = i + 1;
|
||||
let k = n - 1;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
s = nums[i] + nums[j] + nums[k];
|
||||
if (s >= target) {
|
||||
--k;
|
||||
} else {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ class Solution:
|
|||
def threeSumSmaller(self, nums: List[int], target: int) -> int:
|
||||
nums.sort()
|
||||
ans, n = 0, len(nums)
|
||||
for i in range(n):
|
||||
for i in range(n - 2):
|
||||
j, k = i + 1, n - 1
|
||||
while j < k:
|
||||
s = nums[i] + nums[j] + nums[k]
|
||||
if s >= target:
|
||||
k -= 1
|
||||
else:
|
||||
x = nums[i] + nums[j] + nums[k]
|
||||
if x < target:
|
||||
ans += k - j
|
||||
j += 1
|
||||
else:
|
||||
k -= 1
|
||||
return ans
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
function threeSumSmaller(nums: number[], target: number): number {
|
||||
nums.sort((a, b) => a - b);
|
||||
const n = nums.length;
|
||||
let ans = 0;
|
||||
for (let i = 0; i < n - 2; ++i) {
|
||||
let [j, k] = [i + 1, n - 1];
|
||||
while (j < k) {
|
||||
const x = nums[i] + nums[j] + nums[k];
|
||||
if (x < target) {
|
||||
ans += k - j;
|
||||
++j;
|
||||
} else {
|
||||
--k;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
|
@ -253,38 +253,4 @@ function countSubIslands(grid1, grid2) {
|
|||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### 方法二
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
|
||||
def bfs(i: int, j: int) -> int:
|
||||
ok = grid1[i][j]
|
||||
q = deque([(i, j)])
|
||||
grid2[i][j] = 0
|
||||
while q:
|
||||
i, j = q.popleft()
|
||||
for a, b in pairwise(dirs):
|
||||
x, y = i + a, j + b
|
||||
if 0 <= x < m and 0 <= y < n and grid2[x][y]:
|
||||
q.append((x, y))
|
||||
ok = ok & grid1[x][y]
|
||||
grid2[x][y] = 0
|
||||
return ok
|
||||
|
||||
m, n = len(grid1), len(grid1[0])
|
||||
dirs = (-1, 0, 1, 0, -1)
|
||||
return sum(bfs(i, j) for i in range(m) for j in range(n) if grid2[i][j])
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
|
|||
|
|
@ -253,38 +253,4 @@ function countSubIslands(grid1, grid2) {
|
|||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 2
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
|
||||
def bfs(i: int, j: int) -> int:
|
||||
ok = grid1[i][j]
|
||||
q = deque([(i, j)])
|
||||
grid2[i][j] = 0
|
||||
while q:
|
||||
i, j = q.popleft()
|
||||
for a, b in pairwise(dirs):
|
||||
x, y = i + a, j + b
|
||||
if 0 <= x < m and 0 <= y < n and grid2[x][y]:
|
||||
q.append((x, y))
|
||||
ok = ok & grid1[x][y]
|
||||
grid2[x][y] = 0
|
||||
return ok
|
||||
|
||||
m, n = len(grid1), len(grid1[0])
|
||||
dirs = (-1, 0, 1, 0, -1)
|
||||
return sum(bfs(i, j) for i in range(m) for j in range(n) if grid2[i][j])
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
class Solution:
|
||||
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
|
||||
def bfs(i: int, j: int) -> int:
|
||||
ok = grid1[i][j]
|
||||
q = deque([(i, j)])
|
||||
grid2[i][j] = 0
|
||||
while q:
|
||||
i, j = q.popleft()
|
||||
for a, b in pairwise(dirs):
|
||||
x, y = i + a, j + b
|
||||
if 0 <= x < m and 0 <= y < n and grid2[x][y]:
|
||||
q.append((x, y))
|
||||
ok = ok & grid1[x][y]
|
||||
grid2[x][y] = 0
|
||||
return ok
|
||||
|
||||
m, n = len(grid1), len(grid1[0])
|
||||
dirs = (-1, 0, 1, 0, -1)
|
||||
return sum(bfs(i, j) for i in range(m) for j in range(n) if grid2[i][j])
|
||||
|
|
@ -28,7 +28,7 @@ tags:
|
|||
<ul>
|
||||
<li><code>SmallestInfiniteSet()</code> 初始化 <strong>SmallestInfiniteSet</strong> 对象以包含 <strong>所有</strong> 正整数。</li>
|
||||
<li><code>int popSmallest()</code> <strong>移除</strong> 并返回该无限集中的最小整数。</li>
|
||||
<li><code>void addBack(int num)</code> 如果正整数 <code>num</code> <strong>不</strong> 存在于无限集中,则将一个 <code>num</code> <strong>添加</strong> 到该无限集最后。</li>
|
||||
<li><code>void addBack(int num)</code> 如果正整数 <code>num</code> <strong>不</strong> 存在于无限集中,则将一个 <code>num</code> <strong>添加</strong> 到该无限集中。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
|
|
|||
|
|
@ -127,7 +127,9 @@ class Solution {
|
|||
class Solution {
|
||||
public:
|
||||
int deleteGreatestValue(vector<vector<int>>& grid) {
|
||||
for (auto& row : grid) sort(row.begin(), row.end());
|
||||
for (auto& row : grid) {
|
||||
ranges::sort(row);
|
||||
}
|
||||
int ans = 0;
|
||||
for (int j = 0; j < grid[0].size(); ++j) {
|
||||
int t = 0;
|
||||
|
|
|
|||
|
|
@ -74,7 +74,13 @@ The final answer = 10.
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 1
|
||||
### Solution 1: Sorting
|
||||
|
||||
Since each operation involves removing the maximum value from each row and then adding the maximum value to the answer, we can first sort each row.
|
||||
|
||||
Next, we traverse each column, take the maximum value from each column, and add it to the answer.
|
||||
|
||||
The time complexity is $O(m \times n \times \log n)$, and the space complexity is $O(\log n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -115,7 +121,9 @@ class Solution {
|
|||
class Solution {
|
||||
public:
|
||||
int deleteGreatestValue(vector<vector<int>>& grid) {
|
||||
for (auto& row : grid) sort(row.begin(), row.end());
|
||||
for (auto& row : grid) {
|
||||
ranges::sort(row);
|
||||
}
|
||||
int ans = 0;
|
||||
for (int j = 0; j < grid[0].size(); ++j) {
|
||||
int t = 0;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
class Solution {
|
||||
public:
|
||||
int deleteGreatestValue(vector<vector<int>>& grid) {
|
||||
for (auto& row : grid) sort(row.begin(), row.end());
|
||||
for (auto& row : grid) {
|
||||
ranges::sort(row);
|
||||
}
|
||||
int ans = 0;
|
||||
for (int j = 0; j < grid[0].size(); ++j) {
|
||||
int t = 0;
|
||||
|
|
@ -12,4 +14,4 @@ public:
|
|||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ tags:
|
|||
|
||||
我们先用哈希表记录数组中的所有元素,然后枚举数组中的每个元素作为子序列的第一个元素,将该元素不断平方,并判断平方后的结果是否在哈希表中,如果在,则将平方后的结果作为下一个元素,继续判断,直到平方后的结果不在哈希表中,此时判断子序列的长度是否大于 $1$,如果是,则更新答案。
|
||||
|
||||
时间复杂度 $O(n \times \log \log M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度,而 $M$ 为数组 `nums` 中的最大元素。
|
||||
时间复杂度 $O(n \times \log \log M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $M$ 为数组 $\textit{nums}$ 中的元素的最大值。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -171,16 +171,16 @@ func longestSquareStreak(nums []int) int {
|
|||
|
||||
### 方法二:记忆化搜索
|
||||
|
||||
与方法一类似,我们先用哈希表记录数组中的所有元素。然后设计一个函数 $dfs(x)$,表示以 $x$ 为第一个元素的方波的长度。那么答案就是 $max(dfs(x))$,其中 $x$ 为数组 `nums` 中的元素。
|
||||
与方法一类似,我们先用哈希表记录数组中的所有元素。然后设计一个函数 $\textit{dfs}(x)$,表示以 $x$ 为第一个元素的方波的长度。那么答案就是 $\max(\textit{dfs}(x))$,其中 $x$ 为数组 $\textit{nums}$ 中的元素。
|
||||
|
||||
函数 $dfs(x)$ 的计算过程如下:
|
||||
函数 $\textit{dfs}(x)$ 的计算过程如下:
|
||||
|
||||
- 如果 $x$ 不在哈希表中,则返回 $0$。
|
||||
- 否则,返回 $1 + dfs(x^2)$。
|
||||
- 否则,返回 $1 + \textit{dfs}(x^2)$。
|
||||
|
||||
过程中我们可以使用记忆化搜索,即使用哈希表记录函数 $dfs(x)$ 的值,避免重复计算。
|
||||
过程中我们可以使用记忆化搜索,即使用哈希表记录函数 $\textit{dfs}(x)$ 的值,避免重复计算。
|
||||
|
||||
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
|
||||
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
|
|||
|
|
@ -68,7 +68,11 @@ It can be shown that every subsequence of length 4 is not a square streak.
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 1
|
||||
### Solution 1: Hash Table + Enumeration
|
||||
|
||||
We first use a hash table to record all elements in the array. Then, we enumerate each element in the array as the first element of the subsequence, square this element continuously, and check whether the squared result is in the hash table. If it is, we use the squared result as the next element and continue checking until the squared result is not in the hash table. At this point, we check whether the length of the subsequence is greater than $1$. If it is, we update the answer.
|
||||
|
||||
The time complexity is $O(n \times \log \log M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$, and $M$ is the maximum value of the elements in the array $\textit{nums}$.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
@ -165,7 +169,18 @@ func longestSquareStreak(nums []int) int {
|
|||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 2
|
||||
### Solution 2: Memoization Search
|
||||
|
||||
Similar to Solution 1, we first use a hash table to record all elements in the array. Then, we design a function $\textit{dfs}(x)$, which represents the length of the square wave starting with $x$. The answer is $\max(\textit{dfs}(x))$, where $x$ is an element in the array $\textit{nums}$.
|
||||
|
||||
The calculation process of the function $\textit{dfs}(x)$ is as follows:
|
||||
|
||||
- If $x$ is not in the hash table, return $0$.
|
||||
- Otherwise, return $1 + \textit{dfs}(x^2)$.
|
||||
|
||||
During the process, we can use memoization, i.e., use a hash table to record the value of the function $\textit{dfs}(x)$ to avoid redundant calculations.
|
||||
|
||||
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
|
|
|
|||
|
|
@ -168,4 +168,110 @@ function duplicateNumbersXOR(nums: number[]): number {
|
|||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### 方法二:位运算
|
||||
|
||||
由于题目中给出的数字范围是 $1 \leq \textit{nums}[i] \leq 50$,我们可以使用一个 $64$ 位的整数来存储每个数字的出现次数。
|
||||
|
||||
我们定义一个整数 $\textit{mask}$ 来记录每个数字是否出现过。
|
||||
|
||||
接下来,遍历数组 $\textit{nums}$,当某个数字出现两次时,即 $\textit{mask}$ 的二进制表示中第 $x$ 位为 $1$ 时,我们将其与答案进行异或运算。否则,我们将 $\textit{mask}$ 的第 $x$ 位设置为 $1$。
|
||||
|
||||
最后返回答案即可。
|
||||
|
||||
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def duplicateNumbersXOR(self, nums: List[int]) -> int:
|
||||
ans = mask = 0
|
||||
for x in nums:
|
||||
if mask >> x & 1:
|
||||
ans ^= x
|
||||
else:
|
||||
mask |= 1 << x
|
||||
return ans
|
||||
```
|
||||
|
||||
#### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int duplicateNumbersXOR(int[] nums) {
|
||||
int ans = 0;
|
||||
long mask = 0;
|
||||
for (int x : nums) {
|
||||
if ((mask >> x & 1) == 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1L << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### C++
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int duplicateNumbersXOR(vector<int>& nums) {
|
||||
int ans = 0;
|
||||
long long mask = 0;
|
||||
for (int x : nums) {
|
||||
if (mask >> x & 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1LL << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### Go
|
||||
|
||||
```go
|
||||
func duplicateNumbersXOR(nums []int) (ans int) {
|
||||
mask := 0
|
||||
for _, x := range nums {
|
||||
if mask>>x&1 == 1 {
|
||||
ans ^= x
|
||||
} else {
|
||||
mask |= 1 << x
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
#### TypeScript
|
||||
|
||||
```ts
|
||||
function duplicateNumbersXOR(nums: number[]): number {
|
||||
let ans = 0;
|
||||
let mask = 0n;
|
||||
for (const x of nums) {
|
||||
if ((mask >> BigInt(x)) & 1n) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1n << BigInt(x);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
|
|||
|
|
@ -166,4 +166,110 @@ function duplicateNumbersXOR(nums: number[]): number {
|
|||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- solution:start -->
|
||||
|
||||
### Solution 2: Bit Manipulation
|
||||
|
||||
Since the given number range in the problem is $1 \leq \textit{nums}[i] \leq 50$, we can use a $64$-bit integer to store the occurrence of each number.
|
||||
|
||||
We define an integer $\textit{mask}$ to record whether each number has appeared.
|
||||
|
||||
Next, we traverse the array $\textit{nums}$. When a number appears twice, i.e., the $x$-th bit in the binary representation of $\textit{mask}$ is $1$, we perform an XOR operation with the answer. Otherwise, we set the $x$-th bit of $\textit{mask}$ to $1$.
|
||||
|
||||
Finally, we return the answer.
|
||||
|
||||
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
#### Python3
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def duplicateNumbersXOR(self, nums: List[int]) -> int:
|
||||
ans = mask = 0
|
||||
for x in nums:
|
||||
if mask >> x & 1:
|
||||
ans ^= x
|
||||
else:
|
||||
mask |= 1 << x
|
||||
return ans
|
||||
```
|
||||
|
||||
#### Java
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int duplicateNumbersXOR(int[] nums) {
|
||||
int ans = 0;
|
||||
long mask = 0;
|
||||
for (int x : nums) {
|
||||
if ((mask >> x & 1) == 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1L << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### C++
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
int duplicateNumbersXOR(vector<int>& nums) {
|
||||
int ans = 0;
|
||||
long long mask = 0;
|
||||
for (int x : nums) {
|
||||
if (mask >> x & 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1LL << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
#### Go
|
||||
|
||||
```go
|
||||
func duplicateNumbersXOR(nums []int) (ans int) {
|
||||
mask := 0
|
||||
for _, x := range nums {
|
||||
if mask>>x&1 == 1 {
|
||||
ans ^= x
|
||||
} else {
|
||||
mask |= 1 << x
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
#### TypeScript
|
||||
|
||||
```ts
|
||||
function duplicateNumbersXOR(nums: number[]): number {
|
||||
let ans = 0;
|
||||
let mask = 0n;
|
||||
for (const x of nums) {
|
||||
if ((mask >> BigInt(x)) & 1n) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1n << BigInt(x);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
||||
<!-- problem:end -->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
class Solution {
|
||||
public:
|
||||
int duplicateNumbersXOR(vector<int>& nums) {
|
||||
int ans = 0;
|
||||
long long mask = 0;
|
||||
for (int x : nums) {
|
||||
if (mask >> x & 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1LL << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
func duplicateNumbersXOR(nums []int) (ans int) {
|
||||
mask := 0
|
||||
for _, x := range nums {
|
||||
if mask>>x&1 == 1 {
|
||||
ans ^= x
|
||||
} else {
|
||||
mask |= 1 << x
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
class Solution {
|
||||
public int duplicateNumbersXOR(int[] nums) {
|
||||
int ans = 0;
|
||||
long mask = 0;
|
||||
for (int x : nums) {
|
||||
if ((mask >> x & 1) == 1) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1L << x;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
class Solution:
|
||||
def duplicateNumbersXOR(self, nums: List[int]) -> int:
|
||||
ans = mask = 0
|
||||
for x in nums:
|
||||
if mask >> x & 1:
|
||||
ans ^= x
|
||||
else:
|
||||
mask |= 1 << x
|
||||
return ans
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
function duplicateNumbersXOR(nums: number[]): number {
|
||||
let ans = 0;
|
||||
let mask = 0n;
|
||||
for (const x of nums) {
|
||||
if ((mask >> BigInt(x)) & 1n) {
|
||||
ans ^= x;
|
||||
} else {
|
||||
mask |= 1n << BigInt(x);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ tags:
|
|||
|
||||
<!-- problem:start -->
|
||||
|
||||
# [3313. Find the Last Marked Nodes in Tree 🔒](https://leetcode.cn/problems/find-the-last-marked-nodes-in-tree)
|
||||
# [3313. 查找树中最后标记的节点 🔒](https://leetcode.cn/problems/find-the-last-marked-nodes-in-tree)
|
||||
|
||||
[English Version](/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/README_EN.md)
|
||||
|
||||
|
|
@ -17,77 +17,79 @@ tags:
|
|||
|
||||
<!-- description:start -->
|
||||
|
||||
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree.</p>
|
||||
<p>有一棵有 <code>n</code> 个节点,编号从 <code>0</code> 到 <code>n - 1</code> 的 <strong>无向</strong> 树。给定一个长度为 <code>n - 1</code> 的整数数组 <code>edges</code>,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示树中的 <code>u<sub>i</sub></code> 和 <code>v<sub>i</sub></code> 之间有一条边。</p>
|
||||
|
||||
<p>Initially, <strong>all</strong> nodes are <strong>unmarked</strong>. After every second, you mark all unmarked nodes which have <strong>at least</strong> one marked node <em>adjacent</em> to them.</p>
|
||||
<p>一开始,<strong>所有</strong> 节点都 <b>未标记</b>。之后的每一秒,你需要标记所有 <strong>至少</strong> 有一个已标记节点相邻的未标记节点。</p>
|
||||
|
||||
<p>Return an array <code>nodes</code> where <code>nodes[i]</code> is the last node to get marked in the tree, if you mark node <code>i</code> at time <code>t = 0</code>. If <code>nodes[i]</code> has <em>multiple</em> answers for any node <code>i</code>, you can choose<strong> any</strong> one answer.</p>
|
||||
<p>返回一个数组 <code>nodes</code>,表示在时刻 <code>t = 0</code> 标记了节点 <code>i</code>,那么树中最后标记的节点是 <code>nodes[i]</code>。如果对于任意节点 <code>i</code> 有多个 <code>nodes[i]</code>,你可以选择 <strong>任意</strong> 一个作为答案。</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[0,2]]</span></p>
|
||||
<p><span class="example-io"><b>输入:</b>edges = [[0,1],[0,2]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> [2,2,1]</p>
|
||||
<p><b>输出:</b>[2,2,1]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/images/screenshot-2024-06-02-122236.png" style="width: 450px; height: 217px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>, the nodes are marked in the sequence: <code>[0] -> [0,1,2]</code>. Either 1 or 2 can be the answer.</li>
|
||||
<li>For <code>i = 1</code>, the nodes are marked in the sequence: <code>[1] -> [0,1] -> [0,1,2]</code>. Node 2 is marked last.</li>
|
||||
<li>For <code>i = 2</code>, the nodes are marked in the sequence: <code>[2] -> [0,2] -> [0,1,2]</code>. Node 1 is marked last.</li>
|
||||
<li>对于 <code>i = 0</code>,节点以如下序列标记:<code>[0] -> [0,1,2]</code>。1 和 2 都可以是答案。</li>
|
||||
<li>对于 <code>i = 1</code>,节点以如下序列标记:<code>[1] -> [0,1] -> [0,1,2]</code>。节点 2 最后被标记。</li>
|
||||
<li>对于 <code>i = 2</code>,节点以如下序列标记:<code>[2] -> [0,2] -> [0,1,2]</code>。节点 1 最后被标记。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1]]</span></p>
|
||||
<p><span class="example-io"><b>输入:</b>edges = [[0,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> [1,0]</p>
|
||||
<p><b>输出:</b>[1,0]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/images/screenshot-2024-06-02-122249.png" style="width: 350px; height: 180px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>, the nodes are marked in the sequence: <code>[0] -> [0,1]</code>.</li>
|
||||
<li>For <code>i = 1</code>, the nodes are marked in the sequence: <code>[1] -> [0,1]</code>.</li>
|
||||
<li>对于 <code>i = 0</code>,节点以如下序列被标记:<code>[0] -> [0,1]</code>。</li>
|
||||
<li>对于 <code>i = 1</code>,节点以如下序列被标记:<code>[1] -> [0,1]</code>。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[0,2],[2,3],[2,4]]</span></p>
|
||||
<p><span class="example-io"><b>输入:</b>edges = [[0,1],[0,2],[2,3],[2,4]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> [3,3,1,1,1]</p>
|
||||
<p><b>输出:</b>[3,3,1,1,1]</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
<p><strong>解释:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/images/screenshot-2024-06-03-210550.png" style="height: 240px; width: 450px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>For <code>i = 0</code>, the nodes are marked in the sequence: <code>[0] -> [0,1,2] -> [0,1,2,3,4]</code>.</li>
|
||||
<li>For <code>i = 1</code>, the nodes are marked in the sequence: <code>[1] -> [0,1] -> [0,1,2] -> [0,1,2,3,4]</code>.</li>
|
||||
<li>For <code>i = 2</code>, the nodes are marked in the sequence: <code>[2] -> [0,2,3,4] -> [0,1,2,3,4]</code>.</li>
|
||||
<li>For <code>i = 3</code>, the nodes are marked in the sequence: <code>[3] -> [2,3] -> [0,2,3,4] -> [0,1,2,3,4]</code>.</li>
|
||||
<li>For <code>i = 4</code>, the nodes are marked in the sequence: <code>[4] -> [2,4] -> [0,2,3,4] -> [0,1,2,3,4]</code>.</li>
|
||||
<li>对于 <code>i = 0</code>,节点以如下序列被标记:<code>[0] -> [0,1,2] -> [0,1,2,3,4]</code>。</li>
|
||||
<li>对于 <code>i = 1</code>,节点以如下序列被标记:<code>[1] -> [0,1] -> [0,1,2] -> [0,1,2,3,4]</code>。</li>
|
||||
<li>对于 <code>i = 2</code>,节点以如下序列被标记:<code>[2] -> [0,2,3,4] -> [0,1,2,3,4]</code>。</li>
|
||||
<li>对于 <code>i = 3</code>,节点以如下序列被标记:<code>[3] -> [2,3] -> [0,2,3,4] -> [0,1,2,3,4]</code>。</li>
|
||||
<li>对于 <code>i = 4</code>,节点以如下序列被标记:<code>[4] -> [2,4] -> [0,2,3,4] -> [0,1,2,3,4]</code>。</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= edges[i][0], edges[i][1] <= n - 1</code></li>
|
||||
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||||
<li>输入保证 <code>edges</code> 形成一棵合法的树。</li>
|
||||
</ul>
|
||||
|
||||
<!-- description:end -->
|
||||
|
|
|
|||
|
|
@ -3323,7 +3323,7 @@
|
|||
| 3310 | [移除可疑的方法](/solution/3300-3399/3310.Remove%20Methods%20From%20Project/README.md) | `深度优先搜索`,`广度优先搜索`,`图` | 中等 | 第 418 场周赛 |
|
||||
| 3311 | [构造符合图结构的二维矩阵](/solution/3300-3399/3311.Construct%202D%20Grid%20Matching%20Graph%20Layout/README.md) | `图`,`数组`,`哈希表`,`矩阵` | 困难 | 第 418 场周赛 |
|
||||
| 3312 | [查询排序后的最大公约数](/solution/3300-3399/3312.Sorted%20GCD%20Pair%20Queries/README.md) | `数组`,`哈希表`,`数学`,`二分查找`,`组合数学`,`计数`,`数论`,`前缀和` | 困难 | 第 418 场周赛 |
|
||||
| 3313 | [Find the Last Marked Nodes in Tree](/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/README.md) | `树`,`深度优先搜索` | 困难 | 🔒 |
|
||||
| 3313 | [查找树中最后标记的节点](/solution/3300-3399/3313.Find%20the%20Last%20Marked%20Nodes%20in%20Tree/README.md) | `树`,`深度优先搜索` | 困难 | 🔒 |
|
||||
|
||||
## 版权
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue