Skip to content

Commit 97103cc

Browse files
committed
chore: add new lc problems
1 parent 3803e27 commit 97103cc

File tree

23 files changed

+1098
-2
lines changed

23 files changed

+1098
-2
lines changed

solution/2700-2799/2731.Movement of Robots/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<li>当机器人相撞时,它们 <strong>立即改变</strong>&nbsp;它们的前进时间,这个过程不消耗任何时间。</li>
2222
<li>
2323
<p>当两个机器人在同一时刻占据相同的位置时,就会相撞。</p>
24-
2524
<ul>
2625
<li>
2726
<p>例如,如果一个机器人位于位置 0 并往右移动,另一个机器人位于位置 2 并往左移动,下一秒,它们都将占据位置 1,并改变方向。再下一秒钟后,第一个机器人位于位置 0 并往左移动,而另一个机器人位于位置 2 并往右移动。</p>
@@ -31,7 +30,6 @@
3130
</li>
3231
</ul>
3332
</li>
34-
3533
</ul>
3634

3735
<p>&nbsp;</p>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# [2733. 既不是最小值也不是最大值](https://leetcode.cn/problems/neither-minimum-nor-maximum)
2+
3+
[English Version](/solution/2700-2799/2733.Neither%20Minimum%20nor%20Maximum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个整数数组 <code>nums</code> ,数组由 <strong>不同正整数</strong> 组成,请你找出并返回数组中 <strong>任一</strong> 既不是 <strong>最小值</strong> 也不是 <strong>最大值</strong> 的数字,如果不存在这样的数字,返回 <strong><code>-1</code></strong> 。</p>
10+
11+
<p>返回所选整数。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><strong>输入:</strong>nums = [3,2,1,4]
18+
<strong>输出:</strong>2
19+
<strong>解释:</strong>在这个示例中,最小值是 1 ,最大值是 4 。因此,2 或 3 都是有效答案。
20+
</pre>
21+
22+
<p><strong>示例 2:</strong></p>
23+
24+
<pre><strong>输入:</strong>nums = [1,2]
25+
<strong>输出:</strong>-1
26+
<strong>解释:</strong>由于不存在既不是最大值也不是最小值的数字,我们无法选出满足题目给定条件的数字。因此,不存在答案,返回 -1 。
27+
</pre>
28+
29+
<p><strong>示例 3:</strong></p>
30+
31+
<pre><strong>输入:</strong>nums = [2,1,3]
32+
<strong>输出:</strong>2
33+
<strong>解释:</strong>2 既不是最小值,也不是最大值,这个示例只有这一个有效答案。
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>提示:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
42+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
43+
<li><code>nums</code> 中的所有数字互不相同</li>
44+
</ul>
45+
46+
## 解法
47+
48+
<!-- 这里可写通用的实现逻辑 -->
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
<!-- 这里可写当前语言的特殊实现逻辑 -->
55+
56+
```python
57+
class Solution:
58+
def findNonMinOrMax(self, nums: List[int]) -> int:
59+
return -1 if len(nums) < 3 else sorted(nums)[1]
60+
```
61+
62+
```python
63+
class Solution:
64+
def findNonMinOrMax(self, nums: List[int]) -> int:
65+
mi, mx = min(nums), max(nums)
66+
for x in nums:
67+
if x != mi and x != mx:
68+
return x
69+
return -1
70+
```
71+
72+
### **Java**
73+
74+
<!-- 这里可写当前语言的特殊实现逻辑 -->
75+
76+
```java
77+
class Solution {
78+
public int findNonMinOrMax(int[] nums) {
79+
int mi = 100, mx = 0;
80+
for (int x : nums) {
81+
mi = Math.min(mi, x);
82+
mx = Math.max(mx, x);
83+
}
84+
for (int x : nums) {
85+
if (x != mi && x != mx) {
86+
return x;
87+
}
88+
}
89+
return -1;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int findNonMinOrMax(vector<int>& nums) {
100+
int mi = *min_element(nums.begin(), nums.end());
101+
int mx = *max_element(nums.begin(), nums.end());
102+
for (int x : nums) {
103+
if (x != mi && x != mx) {
104+
return x;
105+
}
106+
}
107+
return -1;
108+
}
109+
};
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
func findNonMinOrMax(nums []int) int {
116+
mi, mx := 100, 0
117+
for _, x := range nums {
118+
if x < mi {
119+
mi = x
120+
}
121+
if x > mx {
122+
mx = x
123+
}
124+
}
125+
for _, x := range nums {
126+
if x != mi && x != mx {
127+
return x
128+
}
129+
}
130+
return -1
131+
}
132+
```
133+
134+
### **...**
135+
136+
```
137+
138+
```
139+
140+
<!-- tabs:end -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# [2733. Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum)
2+
3+
[中文文档](/solution/2700-2799/2733.Neither%20Minimum%20nor%20Maximum/README.md)
4+
5+
## Description
6+
7+
<p>Given an integer array <code>nums</code> containing <strong>distinct</strong> <strong>positive</strong> integers, find and return <strong>any</strong> number from the array that is neither the <strong>minimum</strong> nor the <strong>maximum</strong> value in the array, or <strong><code>-1</code></strong> if there is no such number.</p>
8+
9+
<p>Return <em>the selected integer.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [3,2,1,4]
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong> In this example, the minimum value is 1 and the maximum value is 4. Therefore, either 2 or 3 can be valid answers.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,2]
24+
<strong>Output:</strong> -1
25+
<strong>Explanation:</strong> Since there is no number in nums that is neither the maximum nor the minimum, we cannot select a number that satisfies the given condition. Therefore, there is no answer.
26+
</pre>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [2,1,3]
32+
<strong>Output:</strong> 2
33+
<strong>Explanation:</strong> Since 2 is neither the maximum nor the minimum value in nums, it is the only valid answer.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
41+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
42+
<li>All values in <code>nums</code> are distinct</li>
43+
</ul>
44+
45+
## Solutions
46+
47+
<!-- tabs:start -->
48+
49+
### **Python3**
50+
51+
```python
52+
class Solution:
53+
def findNonMinOrMax(self, nums: List[int]) -> int:
54+
return -1 if len(nums) < 3 else sorted(nums)[1]
55+
```
56+
57+
```python
58+
class Solution:
59+
def findNonMinOrMax(self, nums: List[int]) -> int:
60+
mi, mx = min(nums), max(nums)
61+
for x in nums:
62+
if x != mi and x != mx:
63+
return x
64+
return -1
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
class Solution {
71+
public int findNonMinOrMax(int[] nums) {
72+
int mi = 100, mx = 0;
73+
for (int x : nums) {
74+
mi = Math.min(mi, x);
75+
mx = Math.max(mx, x);
76+
}
77+
for (int x : nums) {
78+
if (x != mi && x != mx) {
79+
return x;
80+
}
81+
}
82+
return -1;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int findNonMinOrMax(vector<int>& nums) {
93+
int mi = *min_element(nums.begin(), nums.end());
94+
int mx = *max_element(nums.begin(), nums.end());
95+
for (int x : nums) {
96+
if (x != mi && x != mx) {
97+
return x;
98+
}
99+
}
100+
return -1;
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func findNonMinOrMax(nums []int) int {
109+
mi, mx := 100, 0
110+
for _, x := range nums {
111+
if x < mi {
112+
mi = x
113+
}
114+
if x > mx {
115+
mx = x
116+
}
117+
}
118+
for _, x := range nums {
119+
if x != mi && x != mx {
120+
return x
121+
}
122+
}
123+
return -1
124+
}
125+
```
126+
127+
### **...**
128+
129+
```
130+
131+
```
132+
133+
<!-- tabs:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int findNonMinOrMax(vector<int>& nums) {
4+
int mi = *min_element(nums.begin(), nums.end());
5+
int mx = *max_element(nums.begin(), nums.end());
6+
for (int x : nums) {
7+
if (x != mi && x != mx) {
8+
return x;
9+
}
10+
}
11+
return -1;
12+
}
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func findNonMinOrMax(nums []int) int {
2+
mi, mx := 100, 0
3+
for _, x := range nums {
4+
if x < mi {
5+
mi = x
6+
}
7+
if x > mx {
8+
mx = x
9+
}
10+
}
11+
for _, x := range nums {
12+
if x != mi && x != mx {
13+
return x
14+
}
15+
}
16+
return -1
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int findNonMinOrMax(int[] nums) {
3+
int mi = 100, mx = 0;
4+
for (int x : nums) {
5+
mi = Math.min(mi, x);
6+
mx = Math.max(mx, x);
7+
}
8+
for (int x : nums) {
9+
if (x != mi && x != mx) {
10+
return x;
11+
}
12+
}
13+
return -1;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def findNonMinOrMax(self, nums: List[int]) -> int:
3+
mi, mx = min(nums), max(nums)
4+
for x in nums:
5+
if x != mi and x != mx:
6+
return x
7+
return -1

0 commit comments

Comments
 (0)