最长连续序列(java)

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

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

 最开始我考虑这个题的时候考虑的很简单,就先排序,然后暴力判断

class Solution {
    public int longestConsecutive(int[] nums) {
        int count;
        if(nums!=null){
        count=1;}else count=0;
        Arrays.sort(nums);
        for(int i=0;i<nums.length-1;i++){
            if(nums[i]==nums[i+1]-1){
                count++;
            }
            if(nums[i+1]>nums[i]+1){
                break;
            }
        }
        System.out.println(count);
        return count;
    }
}

测试的时候,我发现我并没有考虑存在多个序列的问题,而是认为从最开始的一定是最长的

于是我加了一个比较不同长度的部分代码

class Solution {
    public int longestConsecutive(int[] nums) {
       int count;int longestcount=0;
        if(nums!=null){
        count=1;}else count=0;
        Arrays.sort(nums);
        for(int i=0;i<nums.length-1;i++){
            
            if(nums[i]==nums[i+1]-1){
                count++;
            }
            if(nums[i+1]>nums[i]+1||i==nums.length-2){
                longestcount=Math.max(longestcount,count);
                count=1;
            }
        }
        if(nums.length==1){
            longestcount=count;
        }
        return longestcount;
    }
}

提交通过

Java中,给定一个字符串数组,求最长连续序列可以采用多种方法,这里提供一种思路,即利用HashSet来帮助查找连续的字符串序列。具体步骤如下: 1. 首先,将字符串数组转换为HashSet,这样可以在O(1)的时间复杂度内查找字符串是否存在于数组中。 2. 然后,遍历数组中的每个元素,对于每个元素,尝试找到以它开始的连续序列。 3. 对于每个元素,检查它的后继字符串是否存在于HashSet中,如果存在,则继续检查下一个后继字符串,同时记录当前序列的长度。 4. 更新最长连续序列的长度,并记录下对应的起始字符串。 5. 最后,根据最长序列的长度和起始字符串,构造出最长连续序列。 下面是一个实现该逻辑的Java代码示例: ```java import java.util.HashSet; import java.util.Set; public class LongestConsecutiveSequence { public static String longestConsecutiveSequence(String[] strings) { Set<String> stringSet = new HashSet<>(); // 将字符串数组转换为HashSet for (String str : strings) { stringSet.add(str); } String longestSequence = ""; int maxLength = 0; // 遍历数组,寻找最长连续序列 for (String str : strings) { // 检查是否为序列的起始元素 if (!stringSet.contains(str + "a")) { int currentLength = 1; String currentStr = str; // 构建序列 while (stringSet.contains(currentStr + "a")) { currentStr += "a"; currentLength++; } // 更新最长序列 if (currentLength > maxLength) { maxLength = currentLength; longestSequence = str; } } } // 根据最长序列的长度,构造出最长连续序列 StringBuilder longestStr = new StringBuilder(); for (int i = 0; i < maxLength; i++) { longestStr.append(longestSequence).append("a"); longestSequence = longestSequence.substring(0, longestSequence.length() - 1); } return longestStr.toString(); } public static void main(String[] args) { String[] strings = {"a", "ba", "bb", "ab", "abc"}; System.out.println("The longest consecutive sequence is: " + longestConsecutiveSequence(strings)); } } ``` 请注意,上述代码中的字符串连接操作`currentStr += "a"`和`longestStr.append(str).append("a")`用于模拟连续性的检查。在实际应用中,可能需要根据实际的连续性条件进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值