Skip to content

Commit 2e06fc7

Browse files
committed
feat: add solutions to lcci problem: No.17.05
No.17.05.Find Longest Subarray
1 parent 6589186 commit 2e06fc7

File tree

11 files changed

+281
-11
lines changed

11 files changed

+281
-11
lines changed

lcci/17.05.Find Longest Subarray/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,31 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
前缀和 + 哈希表。
37+
38+
遍历字符串数组 array,将数字视为 1,字母视为 -1(或者反过来),题目转换为元素和为 0 的最长子数组。
39+
3640
<!-- tabs:start -->
3741

3842
### **Python3**
3943

4044
<!-- 这里可写当前语言的特殊实现逻辑 -->
4145

4246
```python
47+
class Solution:
48+
def findLongestSubarray(self, array: List[str]) -> List[str]:
49+
seen = {0: -1}
50+
t = mx = 0
51+
ans = []
52+
for i, s in enumerate(array):
53+
t += 1 if s.isalpha() else -1
54+
if t in seen:
55+
if mx < i - seen[t]:
56+
mx = i - seen[t]
57+
ans = array[seen[t] + 1: i + 1]
58+
else:
59+
seen[t] = i
60+
return ans
4361

4462
```
4563

@@ -48,7 +66,85 @@
4866
<!-- 这里可写当前语言的特殊实现逻辑 -->
4967

5068
```java
69+
class Solution {
70+
public String[] findLongestSubarray(String[] array) {
71+
Map<Integer, Integer> seen = new HashMap<>();
72+
seen.put(0, -1);
73+
int t = 0, mx = 0;
74+
int j = 0;
75+
for (int i = 0; i < array.length; ++i) {
76+
t += Character.isDigit(array[i].charAt(0)) ? 1 : -1;
77+
if (seen.containsKey(t)) {
78+
if (mx < i - seen.get(t)) {
79+
mx = i - seen.get(t);
80+
j = seen.get(t) + 1;
81+
}
82+
} else {
83+
seen.put(t, i);
84+
}
85+
}
86+
String[] ans = new String[mx];
87+
for (int i = 0; i < mx; ++i) {
88+
ans[i] = array[i + j];
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
vector<string> findLongestSubarray(vector<string>& array) {
101+
unordered_map<int, int> seen;
102+
seen[0] = -1;
103+
int t = 0, mx = 0, j = 0;
104+
for (int i = 0; i < array.size(); ++i)
105+
{
106+
t += isdigit(array[i][0]) ? 1 : -1;
107+
if (seen.count(t))
108+
{
109+
if (mx < i - seen[t])
110+
{
111+
mx = i - seen[t];
112+
j = seen[t] + 1;
113+
}
114+
}
115+
else
116+
{
117+
seen[t] = i;
118+
}
119+
}
120+
return {array.begin() + j, array.begin() + j + mx};
121+
}
122+
};
123+
```
51124
125+
### **Go**
126+
127+
```go
128+
func findLongestSubarray(array []string) []string {
129+
seen := map[int]int{0: -1}
130+
t, mx, j := 0, 0, 0
131+
for i, s := range array {
132+
if unicode.IsDigit(rune(s[0])) {
133+
t++
134+
} else {
135+
t--
136+
}
137+
if k, ok := seen[t]; ok {
138+
if mx < i-k {
139+
mx = i - k
140+
j = k + 1
141+
}
142+
} else {
143+
seen[t] = i
144+
}
145+
}
146+
return array[j : j+mx]
147+
}
52148
```
53149

54150
### **...**

lcci/17.05.Find Longest Subarray/README_EN.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,104 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def findLongestSubarray(self, array: List[str]) -> List[str]:
50+
seen = {0: -1}
51+
t = mx = 0
52+
ans = []
53+
for i, s in enumerate(array):
54+
t += 1 if s.isalpha() else -1
55+
if t in seen:
56+
if mx < i - seen[t]:
57+
mx = i - seen[t]
58+
ans = array[seen[t] + 1: i + 1]
59+
else:
60+
seen[t] = i
61+
return ans
4962
```
5063

5164
### **Java**
5265

5366
```java
67+
class Solution {
68+
public String[] findLongestSubarray(String[] array) {
69+
Map<Integer, Integer> seen = new HashMap<>();
70+
seen.put(0, -1);
71+
int t = 0, mx = 0;
72+
int j = 0;
73+
for (int i = 0; i < array.length; ++i) {
74+
t += Character.isDigit(array[i].charAt(0)) ? 1 : -1;
75+
if (seen.containsKey(t)) {
76+
if (mx < i - seen.get(t)) {
77+
mx = i - seen.get(t);
78+
j = seen.get(t) + 1;
79+
}
80+
} else {
81+
seen.put(t, i);
82+
}
83+
}
84+
String[] ans = new String[mx];
85+
for (int i = 0; i < mx; ++i) {
86+
ans[i] = array[i + j];
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<string> findLongestSubarray(vector<string>& array) {
99+
unordered_map<int, int> seen;
100+
seen[0] = -1;
101+
int t = 0, mx = 0, j = 0;
102+
for (int i = 0; i < array.size(); ++i)
103+
{
104+
t += isdigit(array[i][0]) ? 1 : -1;
105+
if (seen.count(t))
106+
{
107+
if (mx < i - seen[t])
108+
{
109+
mx = i - seen[t];
110+
j = seen[t] + 1;
111+
}
112+
}
113+
else
114+
{
115+
seen[t] = i;
116+
}
117+
}
118+
return {array.begin() + j, array.begin() + j + mx};
119+
}
120+
};
121+
```
54122
123+
### **Go**
124+
125+
```go
126+
func findLongestSubarray(array []string) []string {
127+
seen := map[int]int{0: -1}
128+
t, mx, j := 0, 0, 0
129+
for i, s := range array {
130+
if unicode.IsDigit(rune(s[0])) {
131+
t++
132+
} else {
133+
t--
134+
}
135+
if k, ok := seen[t]; ok {
136+
if mx < i-k {
137+
mx = i - k
138+
j = k + 1
139+
}
140+
} else {
141+
seen[t] = i
142+
}
143+
}
144+
return array[j : j+mx]
145+
}
55146
```
56147

57148
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<string> findLongestSubarray(vector<string>& array) {
4+
unordered_map<int, int> seen;
5+
seen[0] = -1;
6+
int t = 0, mx = 0, j = 0;
7+
for (int i = 0; i < array.size(); ++i)
8+
{
9+
t += isdigit(array[i][0]) ? 1 : -1;
10+
if (seen.count(t))
11+
{
12+
if (mx < i - seen[t])
13+
{
14+
mx = i - seen[t];
15+
j = seen[t] + 1;
16+
}
17+
}
18+
else
19+
{
20+
seen[t] = i;
21+
}
22+
}
23+
return {array.begin() + j, array.begin() + j + mx};
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findLongestSubarray(array []string) []string {
2+
seen := map[int]int{0: -1}
3+
t, mx, j := 0, 0, 0
4+
for i, s := range array {
5+
if unicode.IsDigit(rune(s[0])) {
6+
t++
7+
} else {
8+
t--
9+
}
10+
if k, ok := seen[t]; ok {
11+
if mx < i-k {
12+
mx = i - k
13+
j = k + 1
14+
}
15+
} else {
16+
seen[t] = i
17+
}
18+
}
19+
return array[j : j+mx]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String[] findLongestSubarray(String[] array) {
3+
Map<Integer, Integer> seen = new HashMap<>();
4+
seen.put(0, -1);
5+
int t = 0, mx = 0;
6+
int j = 0;
7+
for (int i = 0; i < array.length; ++i) {
8+
t += Character.isDigit(array[i].charAt(0)) ? 1 : -1;
9+
if (seen.containsKey(t)) {
10+
if (mx < i - seen.get(t)) {
11+
mx = i - seen.get(t);
12+
j = seen.get(t) + 1;
13+
}
14+
} else {
15+
seen.put(t, i);
16+
}
17+
}
18+
String[] ans = new String[mx];
19+
for (int i = 0; i < mx; ++i) {
20+
ans[i] = array[i + j];
21+
}
22+
return ans;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findLongestSubarray(self, array: List[str]) -> List[str]:
3+
seen = {0: -1}
4+
t = mx = 0
5+
ans = []
6+
for i, s in enumerate(array):
7+
t += 1 if s.isalpha() else -1
8+
if t in seen:
9+
if mx < i - seen[t]:
10+
mx = i - seen[t]
11+
ans = array[seen[t] + 1: i + 1]
12+
else:
13+
seen[t] = i
14+
return ans

lcof2/剑指 Offer II 008. 和大于等于 target 的最短子数组/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ public:
147147

148148
for (right = 0; right < nums.size(); right++) {
149149
sum += nums[right];
150-
while(left <= right && sum >= target) {
150+
while (left <= right && sum >= target) {
151151
minlen = min(minlen, right - left + 1);
152152
sum -= nums[left++];
153153
}
154154
}
155155

156-
return minlen == INT_MAX? 0: minlen;
156+
return minlen == INT_MAX ? 0 : minlen;
157157
}
158158
};
159159
```

lcof2/剑指 Offer II 008. 和大于等于 target 的最短子数组/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class Solution {
77

88
for (right = 0; right < nums.size(); right++) {
99
sum += nums[right];
10-
while(left <= right && sum >= target) {
10+
while (left <= right && sum >= target) {
1111
minlen = min(minlen, right - left + 1);
1212
sum -= nums[left++];
1313
}
1414
}
1515

16-
return minlen == INT_MAX? 0: minlen;
16+
return minlen == INT_MAX ? 0 : minlen;
1717
}
1818
};

solution/0200-0299/0209.Minimum Size Subarray Sum/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ public:
178178

179179
for (right = 0; right < nums.size(); right++) {
180180
sum += nums[right];
181-
while(left <= right && sum >= target) {
181+
while (left <= right && sum >= target) {
182182
minlen = min(minlen, right - left + 1);
183183
sum -= nums[left++];
184184
}
185185
}
186186

187-
return minlen == INT_MAX? 0: minlen;
187+
return minlen == INT_MAX ? 0 : minlen;
188188
}
189189
};
190190
```

solution/0200-0299/0209.Minimum Size Subarray Sum/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ public:
155155

156156
for (right = 0; right < nums.size(); right++) {
157157
sum += nums[right];
158-
while(left <= right && sum >= target) {
158+
while (left <= right && sum >= target) {
159159
minlen = min(minlen, right - left + 1);
160160
sum -= nums[left++];
161161
}
162162
}
163163

164-
return minlen == INT_MAX? 0: minlen;
164+
return minlen == INT_MAX ? 0 : minlen;
165165
}
166166
};
167167
```

solution/0200-0299/0209.Minimum Size Subarray Sum/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class Solution {
77

88
for (right = 0; right < nums.size(); right++) {
99
sum += nums[right];
10-
while(left <= right && sum >= target) {
10+
while (left <= right && sum >= target) {
1111
minlen = min(minlen, right - left + 1);
1212
sum -= nums[left++];
1313
}
1414
}
1515

16-
return minlen == INT_MAX? 0: minlen;
16+
return minlen == INT_MAX ? 0 : minlen;
1717
}
1818
};

0 commit comments

Comments
 (0)