leetcode128-最长连续序列

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

请你设计并实现时间复杂度为 O(n) 的算法解决此问题。

示例 1:

输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。

示例 2:

输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9

提示:

  • 0 <= nums.length <= 105

  • -109 <= nums[i] <= 109

思路:

        题目要求时间复杂度要为O(n) ,这就要求不能对数组进行排序,因为使用排序算法时间复杂度一定超过O(n) ,故要直接对无序数组进行操作。

        最简单粗暴的办法就是把数组放到一个Set集合中,Set集合查询数据的时间复杂度为O(1) ,然后设置一个最大值和最小值开始遍历是否在集合中.

//IDEA测试代码
    public static void main(String[] args) {
        int[] nums = {100,4,200,1,3,2};
        System.out.println(longestConsecutive(nums));
    }
 public static int longestConsecutive(int[] nums) {
        if(nums.length == 0) return 0;
        Set<Integer> Set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            Set.add(nums[i]);
        }
        int left = Arrays.stream(nums).min().getAsInt();
        int right = Arrays.stream(nums).max().getAsInt();

        int cnt = 0;
        int max = 0;
      //遍历所有的元素
        for (int i = left; i <=right; i++) {
            if(Set.contains(i))cnt++;
            else{
                max =  Math.max(max,cnt);
                cnt = 0;
            }
        }
        max =  Math.max(max,cnt);
        return max;
    }

        这样的做法会有较高的时间复杂度,因为好多数都是没有必要遍历的,而且一旦出现[0,1,2,4,8,5,6,7,9,3,55,88,77,99,999999999]这样的数据,时间复杂度必然会超标。因此遍历元素的时候要尽量遍历集合中存在的元素。

 public static int longestConsecutive(int[] nums) {
        if(nums.length == 0) return 0;
        Set<Integer> Set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            Set.add(nums[i]);
        }
        //最长子串
        int maxSub = 0;

        //用来遍历数组
        for (int i = 0; i < nums.length; i++) {
            //假如是最小元素
            if(!(Set.contains(nums[i]-1))){
                //开始计数
                int cnt = 1;
                int currentNum = nums[i];

                //继续往后找
                while(Set.contains(currentNum +1)){
                    cnt++;
                    currentNum ++;
                }
                maxSub = Math.max(maxSub,cnt);
            }
        }
        return maxSub;
    }

        提交答案,虽然通过了,但是耗时还是很久,可以继续优化。想必应该是遍历数组的问题,里面会存在大量重复元素,应该直接遍历Set集合,要使用增强for循环来遍历Set集合。

 public static int longestConsecutive(int[] nums) {
        if(nums.length == 0) return 0;
        Set<Integer> Set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            Set.add(nums[i]);
        }
        //最长子串
        int maxSub = 0;

        //用来遍历集合
        for (int num : Set) {
            //假如是最小元素
            if(!(Set.contains(num-1))){
                //开始计数
                int cnt = 1;
                int currentNum = num;

                //继续往后找
                while(Set.contains(currentNum +1)){
                    cnt++;
                    currentNum ++;
                }
                maxSub = Math.max(maxSub,cnt);
            }
        }
        return maxSub;
    }

总结:

        这道题的难度是要将时间复杂度控制在 O(n),因此不能对数组进行排序。我们采用的遍历方法虽然是双重循环,但是时间复杂度依然是 O(n),因为第二层循环只有在满足是最小的前提下才会进入。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值