Skip to content

Commit e344957

Browse files
committed
feat: add solutions to lc problems: No.2395~2398
* No.2395.Find Subarrays With Equal Sum * No.2396.Strictly Palindromic Number * No.2397.Maximum Rows Covered by Columns * No.2398.Maximum Number of Robots Within Budget
1 parent 4f0f6c3 commit e344957

32 files changed

+3930
-2374
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [2395. 和相等的子数组](https://leetcode.cn/problems/find-subarrays-with-equal-sum)
2+
3+
[English Version](/solution/2300-2399/2395.Find%20Subarrays%20With%20Equal%20Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;,判断是否存在&nbsp;<strong>两个</strong>&nbsp;长度为&nbsp;<code>2</code>&nbsp;的子数组且它们的&nbsp;<strong>和</strong>&nbsp;相等。注意,这两个子数组起始位置的下标必须&nbsp;<strong>不相同</strong>&nbsp;。</p>
10+
11+
<p>如果这样的子数组存在,请返回&nbsp;<code>true</code>,否则返回&nbsp;<code>false</code><em>&nbsp;</em>。</p>
12+
13+
<p><strong>子数组</strong> 是一个数组中一段连续非空的元素组成的序列。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre><b>输入:</b>nums = [4,2,4]
20+
<b>输出:</b>true
21+
<b>解释:</b>元素为 [4,2] 和 [2,4] 的子数组有相同的和 6 。
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre><b>输入:</b>nums = [1,2,3,4,5]
27+
<b>输出:</b>false
28+
<b>解释:</b>没有长度为 2 的两个子数组和相等。
29+
</pre>
30+
31+
<p><strong>示例 3:</strong></p>
32+
33+
<pre><b>输入:</b>nums = [0,0,0]
34+
<b>输出:</b>true
35+
<b>解释:</b>子数组 [nums[0],nums[1]] 和 [nums[1],nums[2]] 的和相等,都为 0 。
36+
注意即使子数组的元素相同,这两个子数组也视为不相同的子数组,因为它们在原数组中的起始位置不同。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>2 &lt;= nums.length &lt;= 1000</code></li>
45+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
46+
</ul>
47+
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
**方法一:哈希表**
54+
55+
用哈希表 `s` 记录数组相邻两元素的和。
56+
57+
遍历数组 `nums`,若 `s` 中存在 `nums[i] + nums[i + 1]`,则返回 `true`;否则将 `nums[i] + nums[i + 1]` 加入 `s` 中。
58+
59+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 为数组 `nums` 的长度。
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
class Solution:
69+
def findSubarrays(self, nums: List[int]) -> bool:
70+
s = set()
71+
for a, b in pairwise(nums):
72+
if (v := a + b) in s:
73+
return True
74+
s.add(v)
75+
return False
76+
```
77+
78+
### **Java**
79+
80+
<!-- 这里可写当前语言的特殊实现逻辑 -->
81+
82+
```java
83+
class Solution {
84+
public boolean findSubarrays(int[] nums) {
85+
Set<Integer> s = new HashSet<>();
86+
for (int i = 0; i < nums.length - 1; ++i) {
87+
int v = nums[i] + nums[i + 1];
88+
if (!s.add(v)) {
89+
return true;
90+
}
91+
}
92+
return false;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool findSubarrays(vector<int>& nums) {
103+
unordered_set<int> s;
104+
for (int i = 0; i < nums.size() - 1; ++i) {
105+
int v = nums[i] + nums[i + 1];
106+
if (s.count(v)) return true;
107+
s.insert(v);
108+
}
109+
return false;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func findSubarrays(nums []int) bool {
118+
s := map[int]bool{}
119+
for i := 0; i < len(nums)-1; i++ {
120+
v := nums[i] + nums[i+1]
121+
if s[v] {
122+
return true
123+
}
124+
s[v] = true
125+
}
126+
return false
127+
}
128+
```
129+
130+
### **TypeScript**
131+
132+
```ts
133+
134+
```
135+
136+
### **...**
137+
138+
```
139+
140+
141+
```
142+
143+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [2395. Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum)
2+
3+
[中文文档](/solution/2300-2399/2395.Find%20Subarrays%20With%20Equal%20Sum/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, determine whether there exist <strong>two</strong> subarrays of length <code>2</code> with <strong>equal</strong> sum. Note that the two subarrays must begin at <strong>different</strong> indices.</p>
8+
9+
<p>Return <code>true</code><em> if these subarrays exist, and </em><code>false</code><em> otherwise.</em></p>
10+
11+
<p>A <b>subarray</b> is a contiguous non-empty sequence of elements within an array.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [4,2,4]
18+
<strong>Output:</strong> true
19+
<strong>Explanation:</strong> The subarrays with elements [4,2] and [2,4] have the same sum of 6.
20+
</pre>
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [1,2,3,4,5]
26+
<strong>Output:</strong> false
27+
<strong>Explanation:</strong> No two subarrays of size 2 have the same sum.
28+
</pre>
29+
30+
<p><strong>Example 3:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [0,0,0]
34+
<strong>Output:</strong> true
35+
<strong>Explanation:</strong> The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0.
36+
Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>2 &lt;= nums.length &lt;= 1000</code></li>
44+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
45+
</ul>
46+
47+
48+
## Solutions
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
```python
55+
class Solution:
56+
def findSubarrays(self, nums: List[int]) -> bool:
57+
s = set()
58+
for a, b in pairwise(nums):
59+
if (v := a + b) in s:
60+
return True
61+
s.add(v)
62+
return False
63+
```
64+
65+
### **Java**
66+
67+
```java
68+
class Solution {
69+
public boolean findSubarrays(int[] nums) {
70+
Set<Integer> s = new HashSet<>();
71+
for (int i = 0; i < nums.length - 1; ++i) {
72+
int v = nums[i] + nums[i + 1];
73+
if (!s.add(v)) {
74+
return true;
75+
}
76+
}
77+
return false;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
bool findSubarrays(vector<int>& nums) {
88+
unordered_set<int> s;
89+
for (int i = 0; i < nums.size() - 1; ++i) {
90+
int v = nums[i] + nums[i + 1];
91+
if (s.count(v)) return true;
92+
s.insert(v);
93+
}
94+
return false;
95+
}
96+
};
97+
```
98+
99+
### **Go**
100+
101+
```go
102+
func findSubarrays(nums []int) bool {
103+
s := map[int]bool{}
104+
for i := 0; i < len(nums)-1; i++ {
105+
v := nums[i] + nums[i+1]
106+
if s[v] {
107+
return true
108+
}
109+
s[v] = true
110+
}
111+
return false
112+
}
113+
```
114+
115+
### **TypeScript**
116+
117+
```ts
118+
119+
```
120+
121+
### **...**
122+
123+
```
124+
125+
126+
```
127+
128+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool findSubarrays(vector<int>& nums) {
4+
unordered_set<int> s;
5+
for (int i = 0; i < nums.size() - 1; ++i) {
6+
int v = nums[i] + nums[i + 1];
7+
if (s.count(v)) return true;
8+
s.insert(v);
9+
}
10+
return false;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func findSubarrays(nums []int) bool {
2+
s := map[int]bool{}
3+
for i := 0; i < len(nums)-1; i++ {
4+
v := nums[i] + nums[i+1]
5+
if s[v] {
6+
return true
7+
}
8+
s[v] = true
9+
}
10+
return false
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean findSubarrays(int[] nums) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int i = 0; i < nums.length - 1; ++i) {
5+
int v = nums[i] + nums[i + 1];
6+
if (!s.add(v)) {
7+
return true;
8+
}
9+
}
10+
return false;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findSubarrays(self, nums: List[int]) -> bool:
3+
s = set()
4+
for a, b in pairwise(nums):
5+
if (v := a + b) in s:
6+
return True
7+
s.add(v)
8+
return False

0 commit comments

Comments
 (0)