diff --git a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md new file mode 100644 index 0000000000000..cfc76d33b83bc --- /dev/null +++ b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md @@ -0,0 +1,107 @@ +--- +comments: true +difficulty: 简单 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README.md +--- + + + +# [3158. 求出出现两次数字的 XOR 值](https://leetcode.cn/problems/find-the-xor-of-numbers-which-appear-twice) + +[English Version](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README_EN.md) + +## 题目描述 + + + +

给你一个数组 nums ,数组中的数字 要么 出现一次,要么 出现两次。

+ +

请你返回数组中所有出现两次数字的按位 XOR 值,如果没有数字出现过两次,返回 0 。

+ +

 

+ +

示例 1:

+ +
+

输入:nums = [1,2,1,3]

+ +

输出:1

+ +

解释:

+ +

nums 中唯一出现过两次的数字是 1 。

+
+ +

示例 2:

+ +
+

输入:nums = [1,2,3]

+ +

输出:0

+ +

解释:

+ +

nums 中没有数字出现两次。

+
+ +

示例 3:

+ +
+

输入:nums = [1,2,2,1]

+ +

输出:3

+ +

解释:

+ +

数字 1 和 2 出现过两次。1 XOR 2 == 3 。

+
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md new file mode 100644 index 0000000000000..f4c04ae2250a5 --- /dev/null +++ b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md @@ -0,0 +1,105 @@ +--- +comments: true +difficulty: Easy +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README_EN.md +--- + + + +# [3158. Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice) + +[中文文档](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README.md) + +## Description + + + +

You are given an array nums, where each number in the array appears either once or twice.

+ +

Return the bitwise XOR of all the numbers that appear twice in the array, or 0 if no number appears twice.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,2,1,3]

+ +

Output: 1

+ +

Explanation:

+ +

The only number that appears twice in nums is 1.

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,3]

+ +

Output: 0

+ +

Explanation:

+ +

No number appears twice in nums.

+
+ +

Example 3:

+ +
+

Input: nums = [1,2,2,1]

+ +

Output: 3

+ +

Explanation:

+ +

Numbers 1 and 2 appeared twice. 1 XOR 2 == 3.

+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md new file mode 100644 index 0000000000000..529b8b2df63db --- /dev/null +++ b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md @@ -0,0 +1,104 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README.md +--- + + + +# [3159. 查询数组中元素的出现位置](https://leetcode.cn/problems/find-occurrences-of-an-element-in-an-array) + +[English Version](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README_EN.md) + +## 题目描述 + + + +

给你一个整数数组 nums ,一个整数数组 queries 和一个整数 x 。

+ +

对于每个查询 queries[i] ,你需要找到 nums 中第 queries[i] 个 x 的位置,并返回它的下标。如果数组中 x 的出现次数少于 queries[i] ,该查询的答案为 -1 。

+ +

请你返回一个整数数组 answer ,包含所有查询的答案。

+ +

 

+ +

示例 1:

+ +
+

输入:nums = [1,3,1,7], queries = [1,3,2,4], x = 1

+ +

输出:[0,-1,2,-1]

+ +

解释:

+ + +
+ +

示例 2:

+ +
+

输入:nums = [1,2,3], queries = [10], x = 5

+ +

输出:[-1]

+ +

解释:

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md new file mode 100644 index 0000000000000..180b98cc22cd4 --- /dev/null +++ b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README_EN.md @@ -0,0 +1,102 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README_EN.md +--- + + + +# [3159. Find Occurrences of an Element in an Array](https://leetcode.com/problems/find-occurrences-of-an-element-in-an-array) + +[中文文档](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README.md) + +## Description + + + +

You are given an integer array nums, an integer array queries, and an integer x.

+ +

For each queries[i], you need to find the index of the queries[i]th occurrence of x in the nums array. If there are fewer than queries[i] occurrences of x, the answer should be -1 for that query.

+ +

Return an integer array answer containing the answers to all queries.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1

+ +

Output: [0,-1,2,-1]

+ +

Explanation:

+ + +
+ +

Example 2:

+ +
+

Input: nums = [1,2,3], queries = [10], x = 5

+ +

Output: [-1]

+ +

Explanation:

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md new file mode 100644 index 0000000000000..84a6c54fc515b --- /dev/null +++ b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md @@ -0,0 +1,116 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README.md +--- + + + +# [3160. 所有球里面不同颜色的数目](https://leetcode.cn/problems/find-the-number-of-distinct-colors-among-the-balls) + +[English Version](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README_EN.md) + +## 题目描述 + + + +

给你一个整数 limit 和一个大小为 n x 2 的二维数组 queries 。

+ +

总共有 limit + 1 个球,每个球的编号为 [0, limit] 中一个 互不相同 的数字。一开始,所有球都没有颜色。queries 中每次操作的格式为 [x, y] ,你需要将球 x 染上颜色 y 。每次操作之后,你需要求出所有球中 不同 颜色的数目。

+ +

请你返回一个长度为 n 的数组 result ,其中 result[i] 是第 i 次操作以后不同颜色的数目。

+ +

注意 ,没有染色的球不算作一种颜色。

+ +

 

+ +

示例 1:

+ +
+

输入:limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]

+ +

输出:[1,2,2,3]

+ +

解释:

+ +

+ + +
+ +

示例 2:

+ +
+

输入:limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]

+ +

输出:[1,2,2,3,4]

+ +

解释:

+ +

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md new file mode 100644 index 0000000000000..ebc89d2e6aa16 --- /dev/null +++ b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README_EN.md @@ -0,0 +1,114 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README_EN.md +--- + + + +# [3160. Find the Number of Distinct Colors Among the Balls](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls) + +[中文文档](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README.md) + +## Description + + + +

You are given an integer limit and a 2D array queries of size n x 2.

+ +

There are limit + 1 balls with distinct labels in the range [0, limit]. Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x with the color y. After each query, you need to find the number of distinct colors among the balls.

+ +

Return an array result of length n, where result[i] denotes the number of distinct colors after ith query.

+ +

Note that when answering a query, lack of a color will not be considered as a color.

+ +

 

+

Example 1:

+ +
+

Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]

+ +

Output: [1,2,2,3]

+ +

Explanation:

+ +

+ + +
+ +

Example 2:

+ +
+

Input: limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]

+ +

Output: [1,2,2,3,4]

+ +

Explanation:

+ +

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop.gif b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop.gif new file mode 100644 index 0000000000000..fac064945df6b Binary files /dev/null and b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop.gif differ diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop2.gif b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop2.gif new file mode 100644 index 0000000000000..438c475621f02 Binary files /dev/null and b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/images/ezgifcom-crop2.gif differ diff --git a/solution/3100-3199/3161.Block Placement Queries/README.md b/solution/3100-3199/3161.Block Placement Queries/README.md new file mode 100644 index 0000000000000..8f52d0cc87501 --- /dev/null +++ b/solution/3100-3199/3161.Block Placement Queries/README.md @@ -0,0 +1,112 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3161.Block%20Placement%20Queries/README.md +--- + + + +# [3161. 物块放置查询](https://leetcode.cn/problems/block-placement-queries) + +[English Version](/solution/3100-3199/3161.Block%20Placement%20Queries/README_EN.md) + +## 题目描述 + + + +

有一条无限长的数轴,原点在 0 处,沿着 x 轴  方向无限延伸。

+ +

给你一个二维数组 queries ,它包含两种操作:

+ +
    +
  1. 操作类型 1 :queries[i] = [1, x] 。在距离原点 x 处建一个障碍物。数据保证当操作执行的时候,位置 x 处 没有 任何障碍物。
  2. +
  3. 操作类型 2 :queries[i] = [2, x, sz] 。判断在数轴范围 [0, x] 内是否可以放置一个长度为 sz 的物块,这个物块需要 完全 放置在范围 [0, x] 内。如果物块与任何障碍物有重合,那么这个物块 不能 被放置,但物块可以与障碍物刚好接触。注意,你只是进行查询,并 不是 真的放置这个物块。每个查询都是相互独立的。
  4. +
+ +

请你返回一个 boolean 数组results ,如果第 i 个操作类型 2 的操作你可以放置物块,那么 results[i] 为 true ,否则为 false 。

+ +

 

+ +

示例 1:

+ +
+

输入:queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]

+ +

输出:[false,true,true]

+ +

解释:

+ +

+ +

查询 0 ,在 x = 2 处放置一个障碍物。在 x = 3 之前任何大小不超过 2 的物块都可以被放置。

+
+ +

示例 2:

+ +
+

输入:queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]

+ +

输出:[true,true,false]

+ +

解释:

+ +

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3161.Block Placement Queries/README_EN.md b/solution/3100-3199/3161.Block Placement Queries/README_EN.md new file mode 100644 index 0000000000000..5bc35790ffc0c --- /dev/null +++ b/solution/3100-3199/3161.Block Placement Queries/README_EN.md @@ -0,0 +1,110 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3161.Block%20Placement%20Queries/README_EN.md +--- + + + +# [3161. Block Placement Queries](https://leetcode.com/problems/block-placement-queries) + +[中文文档](/solution/3100-3199/3161.Block%20Placement%20Queries/README.md) + +## Description + + + +

There exists an infinite number line, with its origin at 0 and extending towards the positive x-axis.

+ +

You are given a 2D array queries, which contains two types of queries:

+ +
    +
  1. For a query of type 1, queries[i] = [1, x]. Build an obstacle at distance x from the origin. It is guaranteed that there is no obstacle at distance x when the query is asked.
  2. +
  3. For a query of type 2, queries[i] = [2, x, sz]. Check if it is possible to place a block of size sz anywhere in the range [0, x] on the line, such that the block entirely lies in the range [0, x]. A block cannot be placed if it intersects with any obstacle, but it may touch it. Note that you do not actually place the block. Queries are separate.
  4. +
+ +

Return a boolean array results, where results[i] is true if you can place the block specified in the ith query of type 2, and false otherwise.

+ +

 

+

Example 1:

+ +
+

Input: queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]

+ +

Output: [false,true,true]

+ +

Explanation:

+ +

+ +

For query 0, place an obstacle at x = 2. A block of size at most 2 can be placed before x = 3.

+
+ +

Example 2:

+ +
+

Input: queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]

+ +

Output: [true,true,false]

+ +

Explanation:

+ +

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3100-3199/3161.Block Placement Queries/images/example0block.png b/solution/3100-3199/3161.Block Placement Queries/images/example0block.png new file mode 100644 index 0000000000000..9a9c930243457 Binary files /dev/null and b/solution/3100-3199/3161.Block Placement Queries/images/example0block.png differ diff --git a/solution/3100-3199/3161.Block Placement Queries/images/example1block.png b/solution/3100-3199/3161.Block Placement Queries/images/example1block.png new file mode 100644 index 0000000000000..da243362ecf4c Binary files /dev/null and b/solution/3100-3199/3161.Block Placement Queries/images/example1block.png differ diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index 652387e898722..995b08da6256a 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -28,6 +28,10 @@ comments: true #### 第 131 场双周赛(2024-05-25 22:30, 90 分钟) 参赛人数 2467 +- [3158. 求出出现两次数字的 XOR 值](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README.md) +- [3159. 查询数组中元素的出现位置](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README.md) +- [3160. 所有球里面不同颜色的数目](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README.md) +- [3161. 物块放置查询](/solution/3100-3199/3161.Block%20Placement%20Queries/README.md) #### 第 398 场周赛(2024-05-19 10:30, 90 分钟) 参赛人数 3606 diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index 914890d4f08e1..48d8c36c71e7b 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -31,6 +31,10 @@ If you want to estimate your score changes after the contest ends, you can visit #### Biweekly Contest 131 +- [3158. Find the XOR of Numbers Which Appear Twice](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README_EN.md) +- [3159. Find Occurrences of an Element in an Array](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README_EN.md) +- [3160. Find the Number of Distinct Colors Among the Balls](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README_EN.md) +- [3161. Block Placement Queries](/solution/3100-3199/3161.Block%20Placement%20Queries/README_EN.md) #### Weekly Contest 398 diff --git a/solution/README.md b/solution/README.md index b2da134b77441..d3514718fce88 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3168,6 +3168,10 @@ | 3155 | [可升级服务器的最大数量](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md) | `数组`,`数学`,`二分查找` | 中等 | 🔒 | | 3156 | [员工任务持续时间和并发任务](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md) | `数据库` | 困难 | 🔒 | | 3157 | [找到具有最小和的树的层数](/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/README.md) | | 中等 | 🔒 | +| 3158 | [求出出现两次数字的 XOR 值](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README.md) | | 简单 | 第 131 场双周赛 | +| 3159 | [查询数组中元素的出现位置](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README.md) | | 中等 | 第 131 场双周赛 | +| 3160 | [所有球里面不同颜色的数目](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README.md) | | 中等 | 第 131 场双周赛 | +| 3161 | [物块放置查询](/solution/3100-3199/3161.Block%20Placement%20Queries/README.md) | | 困难 | 第 131 场双周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 33367bab45005..bf67cdf7e6b90 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3166,6 +3166,10 @@ Press Control + F(or Command + F on | 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md) | `Array`,`Math`,`Binary Search` | Medium | 🔒 | | 3156 | [Employee Task Duration and Concurrent Tasks](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md) | `Database` | Hard | 🔒 | | 3157 | [Find the Level of Tree with Minimum Sum](/solution/3100-3199/3157.Find%20the%20Level%20of%20Tree%20with%20Minimum%20Sum/README_EN.md) | | Medium | 🔒 | +| 3158 | [Find the XOR of Numbers Which Appear Twice](/solution/3100-3199/3158.Find%20the%20XOR%20of%20Numbers%20Which%20Appear%20Twice/README_EN.md) | | Easy | Biweekly Contest 131 | +| 3159 | [Find Occurrences of an Element in an Array](/solution/3100-3199/3159.Find%20Occurrences%20of%20an%20Element%20in%20an%20Array/README_EN.md) | | Medium | Biweekly Contest 131 | +| 3160 | [Find the Number of Distinct Colors Among the Balls](/solution/3100-3199/3160.Find%20the%20Number%20of%20Distinct%20Colors%20Among%20the%20Balls/README_EN.md) | | Medium | Biweekly Contest 131 | +| 3161 | [Block Placement Queries](/solution/3100-3199/3161.Block%20Placement%20Queries/README_EN.md) | | Hard | Biweekly Contest 131 | ## Copyright