leetcode 697. Degree of an Array 题解(数组的度)

该博客解析了LeetCode 697题,探讨如何找到具有相同度(最大元素频率)的数组最小子串。作者首先解释了数组度的概念,并分析了寻找最小长度子数组的策略。通过分析多个解题思路,总结出以空间换时间的解决方案,即使用数组记录计数和位置,并在遇到不同度时覆盖,在度相同时选择较长的子数组。

题目

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.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
意思:首先定义了一个数组的度,就是最大频度的那个元素的个数。我们需要找到一个数组的度的最小子串。

地址

https://leetcode.com/contest/leetcode-weekly-contest-54/problems/degree-of-an-array/

分析

这里有几个概念需要理解清晰,才能设计算法。
1. 数组的度,最大的那个元素的个数。(看起来很容易理解。)
2. 数组的子数组,也就是类似字符串的substring。(看起来很容易理解)
3. 数组同度的子数组,可能很多个,至少有一个,就是自身。
4. 找到一个最小长度的子数组。
5. 最小长度的子数组怎么找到呢?满足度的,起始位置之差则是长度。
6. 长度最小即为答案。

分析了上面这些,应该问题差不多可以有眉目了。
http://blog.youkuaiyun.com/neverever01/article/details/78257600
http://blog.youkuaiyun.com/u011809767/article/details/78248289

解题

 public int findShortestSubArray(int[] nums) {

         //transfer one time , we can find the shortest subarray?
         int fre[] = new int[50000]; //each nums fre
         int pos[] = new int[50000]; //each num first pos!
         int maxDegree = -1;
         int minLength = 50000;
         for(int i=0;i<nums.length;i++)
         {
             int val = nums[i];
             fre[val]++;
             if(fre[val]==1)
             {
                 pos[val] = i; //begin for this val
             }
             if(fre[val]>=maxDegree)
             {


                 int temp = i-pos[val]+1;
                 if(fre[val]>maxDegree)
                     minLength = temp;
                 else                        
                 if(temp<minLength)
                     minLength = temp;
                 maxDegree = fre[val];
                // System.out.println(val+",degree:"+fre[val]+",begin:"+pos[val]+",end:"+i+",minlength:"+minLength);
             }
         }

         return minLength;
        }

总结

  1. 审题并总结规律
  2. 以空间换时间,数组计数和位置记录
  3. 不相等的度直接覆盖; 相等的度则取大的。
### 解题思路 LeetCode 第80题要求删除一个有序数组中出现次数超过两次的重复元素,使得每个元素最多出现两次,并返回去重后的数组。此问题可以通过双指针的方法来实现。 - 由于数组是有序的,因此相同的元素一定是连续出现的。 - 使用两个指针:一个用于遍历数组(`i`),另一个用于记录不重复部分的位置(`k`)。 - 如果当前元素 `nums[i]` 不等于 `nums[k]`,说明遇到了一个新的元素,则将其放到 `nums[k+1]` 的位置,并将 `k` 后移一位。 - 如果当前元素与前一个元素相同,则跳过该元素,直到遇到不同的元素为止。 - 最终返回 `k + 1` 作为新数组的长。 ### C语言实现 以下是针对 LeetCode 80 题的 C 语言实现代码: ```c #include <stdio.h> int removeDuplicates(int* nums, int numsSize) { if (numsSize <= 2) return numsSize; // 如果数组小于等于2,直接返回原长 int k = 1; // 指向结果数组的最后一个有效位置 for (int i = 2; i < numsSize; i++) { if (nums[i] != nums[k - 1]) { // 当前元素与结果数组倒数第二个元素不同 nums[++k] = nums[i]; // 将其加入结果数组 } } return k + 1; // 返回新数组 } ``` ### 测试代码 以下是一个简单的测试用例,用于验证上述函数是否正确工作: ```c #include <stdio.h> int main() { int nums[] = {1, 1, 1, 2, 2, 3}; int numsSize = sizeof(nums) / sizeof(nums[0]); int newLength = removeDuplicates(nums, numsSize); printf("New length: %d\n", newLength); printf("Modified array: "); for (int i = 0; i < newLength; i++) { printf("%d ", nums[i]); } printf("\n"); return 0; } ``` ### 输出示例 运行上述测试代码后,输出应为: ``` New length: 5 Modified array: 1 1 2 2 3 ``` ### 算法复杂分析 - **时间复杂**:O(n),其中 n 是数组的长。只需要一次遍历即可完成操作。 - **空间复杂**:O(1),算法在原地修改数组,没有使用额外的空间[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值