|
1 |
| -[xxxx][title] |
| 1 | +[Degree of an Array][title] |
2 | 2 |
|
3 | 3 | ## 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. |
5 | 5 |
|
| 6 | +Your task is to find the smallest possible length of a (contiguous) subarray of `nums`, that has the same degree as `nums`. |
6 | 7 |
|
7 |
| -**Example:** |
| 8 | +**Example 1:** |
8 | 9 |
|
9 | 10 | ```
|
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. |
11 | 18 | ```
|
12 | 19 |
|
13 |
| -**Note:** |
14 |
| -// Note |
| 20 | +**Example 2:** |
15 | 21 |
|
16 |
| -**Tags:** // tags |
| 22 | +``` |
| 23 | +Input: [1,2,2,3,1,4,2] |
| 24 | +Output: 6 |
| 25 | +``` |
17 | 26 |
|
| 27 | +**Note:** |
18 | 28 |
|
19 |
| -## 思路 1 |
20 |
| -// 贴一些关键代码,说一些解题思路 |
21 |
| -// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面) |
22 |
| -```java |
| 29 | +`nums.length` will be between 1 and 50,000. |
23 | 30 |
|
24 |
| -``` |
25 |
| -```javascript |
| 31 | +`nums[i]` will be an integer between 0 and 49,999. |
26 | 32 |
|
27 |
| -``` |
| 33 | +**Tags:** [Array](https://leetcode.com/tag/array/) |
28 | 34 |
|
29 |
| -## 思路 2 |
30 |
| -// 贴一些关键代码,说一些解题思路 |
31 |
| -```java |
| 35 | +## 思路 |
32 | 36 |
|
33 |
| -``` |
| 37 | +题目给出一个非负的整型数组,设定数组中最多的重复数字的个数为degree,要求找出最短的与原字符串有相同的degree的子字符串。遍历数组,将每个数字出现的个数、长度进行记录即可。 |
34 | 38 |
|
35 |
| -## 思路 3 |
36 |
| -// 贴一些关键代码,说一些解题思路 |
37 |
| -```kotlin |
| 39 | +**Java:** |
38 | 40 |
|
| 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 | +} |
39 | 74 | ```
|
40 | 75 |
|
41 | 76 | ## 结语
|
42 |
| - |
| 77 | + |
43 | 78 | 如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
|
44 | 79 |
|
45 |
| -[title]: https://leetcode.com/problems/xxxx |
| 80 | +[title]: https://leetcode.com/problems/degree-of-an-array/description/ |
46 | 81 | [ls]: https://github.com/RichCodersAndMe/LeetCode-Solution
|
0 commit comments