英文:
Find largest consecutive numbers in array and output numbers and how many there is
问题
我的代码如下打印出连续数字的数量。然而,我希望能够打印出数量以及这些数字是什么。
例如:
array = [1, 4, 9, 5, 2, 6]
这将输出:
连续数字的数量为:3
连续数字:[4 5 6]
public static int consecutive(int[] a)
{
HashSet<Integer> values = new HashSet<Integer>();
for (int i : a)
{
values.add(i);
}
int max = 0;
for (int i : values) {
if (values.contains(i - 1))
{
continue;
}
int length = 0;
while (values.contains(i++))
{
length++;
}
max = Math.max(max, length);
}
return max;
}
英文:
My code below prints out how many consecutive numbers there is. However, I'm looking to print out how many there is as well as what these numbers are.
e.g.
array = [1, 4, 9, 5, 2, 6]
This would output:
Number of consecutive numbers is: 3
Consecutive numbers: [4 5 6]
public static int consecutive(int[] a)
{
HashSet<Integer> values = new HashSet<Integer>();
for (int i :a)
{
values.add(i);
}
int max = 0;
for (int i : values) {
if (values.contains(i - 1))
{
continue;
}
int length = 0;
while (values.contains(i++))
{
length++;
}
max = Math.max(max, length);
}
return max;
}
答案1
得分: 1
按照以下方式进行操作:
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("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 9 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
longestConsecutive = longestConsecutiveList(null);
System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + 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;
// 将数组的元素添加到有序集合中
for (int i : a) {
values.add(i);
}
// 创建迭代器以遍历有序集合
Iterator<Integer> itr = values.iterator();
// 从有序集合中获取第一个元素,将其赋值给value,并将其添加到tempList中。因为tempList只有一个元素
if (itr.hasNext()) {
value = itr.next();
tempList.add(value);
}
// 遍历有序集合的剩余元素(从第二个元素开始)
while (itr.hasNext()) {
// 从有序集合中获取下一个元素,将其赋值给temp
temp = itr.next();
// 如果temp - value = 1,则将temp添加到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;
}
}
输出:
最长连续整数列表:[4, 5, 6],数量:3
最长连续整数列表:[1, 2, 3, 4, 5],数量:5
最长连续整数列表:[9, 10, 11, 12],数量:4
最长连续整数列表:[10, 11, 12],数量:3
最长连续整数列表:[7, 8, 9],数量:3
最长连续整数列表:[9],数量:1
最长连续整数列表:[1, 2],数量:2
最长连续整数列表:[1, 2, 3],数量:3
最长连续整数列表:[],数量:0
代码中有详细的注释,如有疑问或问题,请随时提问。
英文:
Do it as follows:
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;
}
}
Output:
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
I have put enough comments in the code for easy understanding. Feel free to comment in case of any doubt/issue.
答案2
得分: 0
private static int consecutive(int[] a) {
Set<Integer> values;
// 用于存储连续数字的列表
List<Integer> nums = new ArrayList<>();
values = Arrays.stream(a).boxed().collect(Collectors.toSet());
int max = 0;
for (int i : values) {
if (values.contains(i - 1)) {
continue;
}
// 用于存储每个序列的内部列表
List<Integer> temp = new ArrayList<>();
// 将 i++ 移到循环内部,因为需要将值存储起来
while (values.contains(i)) {
temp.add(i);
i++;
}
// 如果内部列表较大,则进行替换
if (nums.size() <= temp.size()) {
nums = temp;
}
max = Math.max(max, temp.size());
}
System.out.println(nums);
return max;
}
英文:
private static int consecutive(int[] a) {
Set<Integer> values;
// to store the consecutive numbers
List<Integer> nums = new ArrayList<>();
values = Arrays.stream(a).boxed().collect(Collectors.toSet());
int max = 0;
for (int i : values) {
if (values.contains(i - 1)) {
continue;
}
// inner list for each sequemce
List<Integer> temp = new ArrayList<>();
// moved i++ inside the loop because the value is required to store
while (values.contains(i)) {
temp.add(i);
i++;
}
// if the inner list is larger, replace
if (nums.size() <= temp.size()) {
nums = temp;
}
max = Math.max(max, temp.size());
}
System.out.println(nums);
return max;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论