Skip to content

Commit 83d5412

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0697
1 parent fe6be61 commit 83d5412

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

solution/0600-0699/0697.Degree of an Array/README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,68 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
遍历数组,用哈希表记录数组每个元素出现的次数,以及首次、末次出现的位置。然后遍历哈希表,获取元素出现次数最多(可能有多个)且首末位置差最小的数。
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

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

4850
```python
49-
51+
class Solution:
52+
def findShortestSubArray(self, nums: List[int]) -> int:
53+
mapper = {}
54+
for i, v in enumerate(nums):
55+
if v in mapper:
56+
arr = mapper[v]
57+
arr[0] += 1
58+
arr[2] = i
59+
else:
60+
arr = [1, i, i]
61+
mapper[v] = arr
62+
max_degree = min_len = 0
63+
for arr in mapper.values():
64+
if max_degree < arr[0]:
65+
max_degree = arr[0]
66+
min_len = arr[2] - arr[1] + 1
67+
elif max_degree == arr[0]:
68+
min_len = min(min_len, arr[2] - arr[1] + 1)
69+
return min_len
5070
```
5171

5272
### **Java**
5373

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

5676
```java
57-
77+
class Solution {
78+
public int findShortestSubArray(int[] nums) {
79+
Map<Integer, int[]> mapper = new HashMap<>();
80+
for (int i = 0, n = nums.length; i < n; ++i) {
81+
if (mapper.containsKey(nums[i])) {
82+
int[] arr = mapper.get(nums[i]);
83+
++arr[0];
84+
arr[2] = i;
85+
} else {
86+
int[] arr = new int[]{1, i, i};
87+
mapper.put(nums[i], arr);
88+
}
89+
}
90+
91+
int maxDegree = 0, minLen = 0;
92+
for (Map.Entry<Integer, int[]> entry : mapper.entrySet()) {
93+
int[] arr = entry.getValue();
94+
if (maxDegree < arr[0]) {
95+
maxDegree = arr[0];
96+
minLen = arr[2] - arr[1] + 1;
97+
} else if (maxDegree == arr[0]) {
98+
minLen = Math.min(minLen, arr[2] - arr[1] + 1);
99+
}
100+
}
101+
return minLen;
102+
}
103+
}
58104
```
59105

60106
### **...**

solution/0600-0699/0697.Degree of an Array/README_EN.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,57 @@ The shortest length is 2. So return 2.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def findShortestSubArray(self, nums: List[int]) -> int:
62+
mapper = {}
63+
for i, v in enumerate(nums):
64+
if v in mapper:
65+
arr = mapper[v]
66+
arr[0] += 1
67+
arr[2] = i
68+
else:
69+
arr = [1, i, i]
70+
mapper[v] = arr
71+
max_degree = min_len = 0
72+
for arr in mapper.values():
73+
if max_degree < arr[0]:
74+
max_degree = arr[0]
75+
min_len = arr[2] - arr[1] + 1
76+
elif max_degree == arr[0]:
77+
min_len = min(min_len, arr[2] - arr[1] + 1)
78+
return min_len
6179
```
6280

6381
### **Java**
6482

6583
```java
66-
84+
class Solution {
85+
public int findShortestSubArray(int[] nums) {
86+
Map<Integer, int[]> mapper = new HashMap<>();
87+
for (int i = 0, n = nums.length; i < n; ++i) {
88+
if (mapper.containsKey(nums[i])) {
89+
int[] arr = mapper.get(nums[i]);
90+
++arr[0];
91+
arr[2] = i;
92+
} else {
93+
int[] arr = new int[]{1, i, i};
94+
mapper.put(nums[i], arr);
95+
}
96+
}
97+
98+
int maxDegree = 0, minLen = 0;
99+
for (Map.Entry<Integer, int[]> entry : mapper.entrySet()) {
100+
int[] arr = entry.getValue();
101+
if (maxDegree < arr[0]) {
102+
maxDegree = arr[0];
103+
minLen = arr[2] - arr[1] + 1;
104+
} else if (maxDegree == arr[0]) {
105+
minLen = Math.min(minLen, arr[2] - arr[1] + 1);
106+
}
107+
}
108+
return minLen;
109+
}
110+
}
67111
```
68112

69113
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int findShortestSubArray(int[] nums) {
3+
Map<Integer, int[]> mapper = new HashMap<>();
4+
for (int i = 0, n = nums.length; i < n; ++i) {
5+
if (mapper.containsKey(nums[i])) {
6+
int[] arr = mapper.get(nums[i]);
7+
++arr[0];
8+
arr[2] = i;
9+
} else {
10+
int[] arr = new int[]{1, i, i};
11+
mapper.put(nums[i], arr);
12+
}
13+
}
14+
15+
int maxDegree = 0, minLen = 0;
16+
for (Map.Entry<Integer, int[]> entry : mapper.entrySet()) {
17+
int[] arr = entry.getValue();
18+
if (maxDegree < arr[0]) {
19+
maxDegree = arr[0];
20+
minLen = arr[2] - arr[1] + 1;
21+
} else if (maxDegree == arr[0]) {
22+
minLen = Math.min(minLen, arr[2] - arr[1] + 1);
23+
}
24+
}
25+
return minLen;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def findShortestSubArray(self, nums: List[int]) -> int:
3+
mapper = {}
4+
for i, v in enumerate(nums):
5+
if v in mapper:
6+
arr = mapper[v]
7+
arr[0] += 1
8+
arr[2] = i
9+
else:
10+
arr = [1, i, i]
11+
mapper[v] = arr
12+
max_degree = min_len = 0
13+
for arr in mapper.values():
14+
if max_degree < arr[0]:
15+
max_degree = arr[0]
16+
min_len = arr[2] - arr[1] + 1
17+
elif max_degree == arr[0]:
18+
min_len = min(min_len, arr[2] - arr[1] + 1)
19+
return min_len

0 commit comments

Comments
 (0)