Java 查找数组中最大的连续数字,并输出数字和数量

Java 查找数组中最大的连续数字,并输出数字和数量,java,arrays,list,Java,Arrays,List,我下面的代码打印出有多少个连续的数字。然而,我希望打印出有多少以及这些数字是什么 e、 g。 数组=[1,4,9,5,2,6] 这将产生: 连续数字的数量为:3 连续数字:[4 5 6] 公共静态int连续(int[]a) { HashSet值=新的HashSet(); 对于(int i:a) { 增加(i); } int max=0; for(int i:值){ if(值包含(i-1)) { 继续; } 整数长度=0; while(values.contains(i++)) { 长度++; }

我下面的代码打印出有多少个连续的数字。然而,我希望打印出有多少以及这些数字是什么

e、 g。 数组=[1,4,9,5,2,6]

这将产生:

连续数字的数量为:3

连续数字:[4 5 6]

公共静态int连续(int[]a)
{
HashSet值=新的HashSet();
对于(int i:a)
{
增加(i);
}
int max=0;
for(int i:值){
if(值包含(i-1))
{
继续;
}
整数长度=0;
while(values.contains(i++))
{
长度++;
}
最大值=数学最大值(最大值,长度);
}
返回最大值;
}
私有静态int连续(int[]a){
设定值;
//存储连续的数字
List nums=new ArrayList();
values=Arrays.stream(a.boxed().collect(Collectors.toSet());
int max=0;
for(int i:值){
if(值包含(i-1)){
继续;
}
//每个序列的内部列表
List temp=new ArrayList();
//将i++移动到循环中,因为需要存储该值
while(values.contains(i)){
临时添加(i);
i++;
}
//如果内部列表较大,请更换
if(nums.size()
私有静态int连续(int[]a){
设定值;
//存储连续的数字
List nums=new ArrayList();
values=Arrays.stream(a.boxed().collect(Collectors.toSet());
int max=0;
for(int i:值){
if(值包含(i-1)){
继续;
}
//每个序列的内部列表
List temp=new ArrayList();
//将i++移动到循环中,因为需要存储该值
while(values.contains(i)){
临时添加(i);
i++;
}
//如果内部列表较大,请更换
如果(nums.size()按如下方式执行:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        // Tests
        List<Integer> longestConsecutive;

        longestConsecutive = longestConsecutiveList(new int[] { 1, 4, 9, 5, 2, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(null);
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

    }

    public static List<Integer> longestConsecutiveList(int[] a) {
        if (a == null) {
            return new ArrayList<Integer>();
        }
        Set<Integer> values = new TreeSet<Integer>();
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> tempList = new ArrayList<Integer>();
        int value = 0, temp = 0;

        // Add the elements of the array to the sorted set
        for (int i : a) {
            values.add(i);
        }

        // Create an iterator to navigate the sorted set
        Iterator<Integer> itr = values.iterator();

        // Get the first element from the sorted set, assign it to value and add it to
        // tempList. Since tempList has one element
        if (itr.hasNext()) {
            value = itr.next();
            tempList.add(value);
        }

        // Navigate the rest (2nd element onwards) of the sorted set
        while (itr.hasNext()) {
            // Get the next element from the sorted set and assign it to temp
            temp = itr.next();

            // If temp - value = 1, add temp to tempList
            if (temp - value == 1) {
                tempList.add(temp);
            } else if (tempList.size() >= list.size()) {
                list = tempList;
                tempList = new ArrayList<Integer>();
                tempList.add(temp);
            } else {
                tempList = new ArrayList<Integer>();
            }
            value = temp;
        }
        return list.size() > tempList.size() ? list : tempList;
    }
}
为了便于理解,我已经在代码中添加了足够多的注释。如果有任何疑问/问题,请随时进行注释。

按照以下步骤操作:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        // Tests
        List<Integer> longestConsecutive;

        longestConsecutive = longestConsecutiveList(new int[] { 1, 4, 9, 5, 2, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(null);
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

    }

    public static List<Integer> longestConsecutiveList(int[] a) {
        if (a == null) {
            return new ArrayList<Integer>();
        }
        Set<Integer> values = new TreeSet<Integer>();
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> tempList = new ArrayList<Integer>();
        int value = 0, temp = 0;

        // Add the elements of the array to the sorted set
        for (int i : a) {
            values.add(i);
        }

        // Create an iterator to navigate the sorted set
        Iterator<Integer> itr = values.iterator();

        // Get the first element from the sorted set, assign it to value and add it to
        // tempList. Since tempList has one element
        if (itr.hasNext()) {
            value = itr.next();
            tempList.add(value);
        }

        // Navigate the rest (2nd element onwards) of the sorted set
        while (itr.hasNext()) {
            // Get the next element from the sorted set and assign it to temp
            temp = itr.next();

            // If temp - value = 1, add temp to tempList
            if (temp - value == 1) {
                tempList.add(temp);
            } else if (tempList.size() >= list.size()) {
                list = tempList;
                tempList = new ArrayList<Integer>();
                tempList.add(temp);
            } else {
                tempList = new ArrayList<Integer>();
            }
            value = temp;
        }
        return list.size() > tempList.size() ? list : tempList;
    }
}

我已经在代码中添加了足够多的注释,以便于理解。如果有任何疑问/问题,请随时发表评论。

问题到底是什么?您可以有重复的吗?在[3,4,4,5]中有多少连续的数字?问题到底是什么?您可以有重复的吗?在[3,4,4,5]中有多少连续的数字?当我输入不同范围的数字时,有时会输出“空”。我的样本输入是“9,7,3,8,1”。知道为什么吗?@pabs095-我已经解决了这个问题。如果有任何疑问/问题,请随时发表评论。现在工作得很好。谢谢。如果重复项也要计算,如何调整此解决方案?例如,如果存在[1,1,2,2]?当我输入不同范围的数字时,它有时会输出“null”。我的示例输入是“9,7,3,8,1”。你知道为什么吗?@pabs095-我已经解决了这个问题。如果有任何疑问/问题,请随时发表评论。现在工作很好。谢谢。如果重复项也要计算,如何调整此解决方案?例如,如果存在[1,1,2,2]?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        // Tests
        List<Integer> longestConsecutive;

        longestConsecutive = longestConsecutiveList(new int[] { 1, 4, 9, 5, 2, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 9 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

        longestConsecutive = longestConsecutiveList(null);
        System.out.println(
                "Logest list of consecutive integers: " + longestConsecutive + ", Count: " + longestConsecutive.size());

    }

    public static List<Integer> longestConsecutiveList(int[] a) {
        if (a == null) {
            return new ArrayList<Integer>();
        }
        Set<Integer> values = new TreeSet<Integer>();
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> tempList = new ArrayList<Integer>();
        int value = 0, temp = 0;

        // Add the elements of the array to the sorted set
        for (int i : a) {
            values.add(i);
        }

        // Create an iterator to navigate the sorted set
        Iterator<Integer> itr = values.iterator();

        // Get the first element from the sorted set, assign it to value and add it to
        // tempList. Since tempList has one element
        if (itr.hasNext()) {
            value = itr.next();
            tempList.add(value);
        }

        // Navigate the rest (2nd element onwards) of the sorted set
        while (itr.hasNext()) {
            // Get the next element from the sorted set and assign it to temp
            temp = itr.next();

            // If temp - value = 1, add temp to tempList
            if (temp - value == 1) {
                tempList.add(temp);
            } else if (tempList.size() >= list.size()) {
                list = tempList;
                tempList = new ArrayList<Integer>();
                tempList.add(temp);
            } else {
                tempList = new ArrayList<Integer>();
            }
            value = temp;
        }
        return list.size() > tempList.size() ? list : tempList;
    }
}
Logest list of consecutive integers: [4, 5, 6], Count: 3
Logest list of consecutive integers: [1, 2, 3, 4, 5], Count: 5
Logest list of consecutive integers: [9, 10, 11, 12], Count: 4
Logest list of consecutive integers: [10, 11, 12], Count: 3
Logest list of consecutive integers: [7, 8, 9], Count: 3
Logest list of consecutive integers: [9], Count: 1
Logest list of consecutive integers: [1, 2], Count: 2
Logest list of consecutive integers: [1, 2, 3], Count: 3
Logest list of consecutive integers: [], Count: 0