Skip to content

Commit 4c65ff1

Browse files
committed
feat: add the solution of Degree of an Array(697) with Java.
1 parent 704368b commit 4c65ff1

File tree

3 files changed

+103
-24
lines changed

3 files changed

+103
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
| [693][693-question] | [Binary Number with Alternating Bits][693-tips] | [][693-java] | [][693-js] | |
7979
| [695][695-question] | [Max Area of Island][695-tips] | [][695-java] | [][695-js] | |
8080
| [696][696-question] | [Count Binary Substrings][696-tips] | [][696-java] | [][696-js] | |
81-
| [697][697-question] | [Degree of an Array][697-tips] | | [][697-js] | |
81+
| [697][697-question] | [Degree of an Array][697-tips] | [][697-java] | [][697-js] | |
8282
| [717][717-question] | [1-bit and 2-bit Characters][717-tips] | | [][717-js] | |
8383
| [720][720-question] | [Longest Word in Dictionary][720-tips] | | [][720-js] | |
8484
| [724][724-question] | [Find Pivot Index][724-tips] | | [][724-js] | |
@@ -476,6 +476,7 @@
476476
[693-java]: ./src/_693/Solution.java
477477
[695-java]: ./src/_695/Solution.java
478478
[696-java]: ./src/_696/Solution.java
479+
[697-java]: ./src/_697/Solution.java
479480
[728-java]: ./src/_728/Solution.java
480481
[771-java]: ./src/_771/Solution.java
481482
[804-java]: ./src/_804/Solution.java

src/_697/Solution.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package _697;
2+
3+
import java.util.HashMap;
4+
5+
class Solution {
6+
public int findShortestSubArray(int[] nums) {
7+
int max = 0;
8+
for (int num : nums) {
9+
max = Math.max(num, max);
10+
}
11+
12+
HashMap<Integer, Integer> firstPositions = new HashMap<>(max);
13+
HashMap<Integer, Integer> degrees = new HashMap<>(max);
14+
int maxDegree = 1;
15+
int minLen = 1;
16+
17+
int degree;
18+
int len;
19+
for (int i = 0; i < nums.length; i++) {
20+
firstPositions.putIfAbsent(nums[i], i);
21+
degree = degrees.getOrDefault(nums[i], 0);
22+
degrees.put(nums[i], ++degree);
23+
if (degree < maxDegree) {
24+
continue;
25+
}
26+
27+
len = i - firstPositions.get(nums[i]) + 1;
28+
if (degree > maxDegree) {
29+
maxDegree = degree;
30+
minLen = len;
31+
} else if (degree == maxDegree && minLen > len) {
32+
minLen = len;
33+
}
34+
}
35+
36+
return minLen;
37+
}
38+
39+
public static void main(String[] args) {
40+
Solution solution = new Solution();
41+
System.out.println(solution.findShortestSubArray(new int[] {1, 2, 2, 3, 1, 4, 2}));
42+
}
43+
}

tips/697/README.md

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,81 @@
1-
[xxxx][title]
1+
[Degree of an Array][title]
22

33
## Description
4-
// 抄题目
4+
Given a non-empty array of non-negative integers `nums`, the **degree** of this array is defined as the maximum frequency of any one of its elements.
55

6+
Your task is to find the smallest possible length of a (contiguous) subarray of `nums`, that has the same degree as `nums`.
67

7-
**Example:**
8+
**Example 1:**
89

910
```
10-
// 抄Example
11+
Input: [1, 2, 2, 3, 1]
12+
Output: 2
13+
Explanation:
14+
The input array has a degree of 2 because both elements 1 and 2 appear twice.
15+
Of the subarrays that have the same degree:
16+
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
17+
The shortest length is 2. So return 2.
1118
```
1219

13-
**Note:**
14-
// Note
20+
**Example 2:**
1521

16-
**Tags:** // tags
22+
```
23+
Input: [1,2,2,3,1,4,2]
24+
Output: 6
25+
```
1726

27+
**Note:**
1828

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
29+
`nums.length` will be between 1 and 50,000.
2330

24-
```
25-
```javascript
31+
`nums[i]` will be an integer between 0 and 49,999.
2632

27-
```
33+
**Tags:** [Array](https://leetcode.com/tag/array/)
2834

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
35+
## 思路
3236

33-
```
37+
题目给出一个非负的整型数组,设定数组中最多的重复数字的个数为degree,要求找出最短的与原字符串有相同的degree的子字符串。遍历数组,将每个数字出现的个数、长度进行记录即可。
3438

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
39+
**Java:**
3840

41+
```java
42+
public int findShortestSubArray(int[] nums) {
43+
int max = 0;
44+
for (int num : nums) {
45+
max = Math.max(num, max);
46+
}
47+
48+
HashMap<Integer, Integer> firstPositions = new HashMap<>(max);
49+
HashMap<Integer, Integer> degrees = new HashMap<>(max);
50+
int maxDegree = 1;
51+
int minLen = 1;
52+
53+
int degree;
54+
int len;
55+
for (int i = 0; i < nums.length; i++) {
56+
firstPositions.putIfAbsent(nums[i], i);
57+
degree = degrees.getOrDefault(nums[i], 0);
58+
degrees.put(nums[i], ++degree);
59+
if (degree < maxDegree) {
60+
continue;
61+
}
62+
63+
len = i - firstPositions.get(nums[i]) + 1;
64+
if (degree > maxDegree) {
65+
maxDegree = degree;
66+
minLen = len;
67+
} else if (degree == maxDegree && minLen > len) {
68+
minLen = len;
69+
}
70+
}
71+
72+
return minLen;
73+
}
3974
```
4075

4176
## 结语
42-
77+
4378
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4479

45-
[title]: https://leetcode.com/problems/xxxx
80+
[title]: https://leetcode.com/problems/degree-of-an-array/description/
4681
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)