找出数组中的最大连续数字,输出数字以及连续数字的数量。

huangapple go评论165阅读模式
英文:

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]

  1. public static int consecutive(int[] a)
  2. {
  3. HashSet<Integer> values = new HashSet<Integer>();
  4. for (int i : a)
  5. {
  6. values.add(i);
  7. }
  8. int max = 0;
  9. for (int i : values) {
  10. if (values.contains(i - 1))
  11. {
  12. continue;
  13. }
  14. int length = 0;
  15. while (values.contains(i++))
  16. {
  17. length++;
  18. }
  19. max = Math.max(max, length);
  20. }
  21. return max;
  22. }
英文:

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]

  1. public static int consecutive(int[] a)
  2. {
  3. HashSet&lt;Integer&gt; values = new HashSet&lt;Integer&gt;();
  4. for (int i :a)
  5. {
  6. values.add(i);
  7. }
  8. int max = 0;
  9. for (int i : values) {
  10. if (values.contains(i - 1))
  11. {
  12. continue;
  13. }
  14. int length = 0;
  15. while (values.contains(i++))
  16. {
  17. length++;
  18. }
  19. max = Math.max(max, length);
  20. }
  21. return max;
  22. }

答案1

得分: 1

按照以下方式进行操作:

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.TreeSet;
  6. public class Main {
  7. public static void main(String[] args) {
  8. // Tests
  9. List<Integer> longestConsecutive;
  10. longestConsecutive = longestConsecutiveList(new int[] { 1, 4, 9, 5, 2, 6 });
  11. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  12. longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
  13. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  14. longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
  15. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  16. longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
  17. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  18. longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
  19. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  20. longestConsecutive = longestConsecutiveList(new int[] { 9 });
  21. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  22. longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
  23. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  24. longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
  25. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  26. longestConsecutive = longestConsecutiveList(null);
  27. System.out.println("最长连续整数列表:" + longestConsecutive + ",数量:" + longestConsecutive.size());
  28. }
  29. public static List<Integer> longestConsecutiveList(int[] a) {
  30. if (a == null) {
  31. return new ArrayList<Integer>();
  32. }
  33. Set<Integer> values = new TreeSet<Integer>();
  34. List<Integer> list = new ArrayList<Integer>();
  35. List<Integer> tempList = new ArrayList<Integer>();
  36. int value = 0, temp = 0;
  37. // 将数组的元素添加到有序集合中
  38. for (int i : a) {
  39. values.add(i);
  40. }
  41. // 创建迭代器以遍历有序集合
  42. Iterator<Integer> itr = values.iterator();
  43. // 从有序集合中获取第一个元素,将其赋值给value,并将其添加到tempList中。因为tempList只有一个元素
  44. if (itr.hasNext()) {
  45. value = itr.next();
  46. tempList.add(value);
  47. }
  48. // 遍历有序集合的剩余元素(从第二个元素开始)
  49. while (itr.hasNext()) {
  50. // 从有序集合中获取下一个元素,将其赋值给temp
  51. temp = itr.next();
  52. // 如果temp - value = 1,则将temp添加到tempList中
  53. if (temp - value == 1) {
  54. tempList.add(temp);
  55. } else if (tempList.size() >= list.size()) {
  56. list = tempList;
  57. tempList = new ArrayList<Integer>();
  58. tempList.add(temp);
  59. } else {
  60. tempList = new ArrayList<Integer>();
  61. }
  62. value = temp;
  63. }
  64. return list.size() > tempList.size() ? list : tempList;
  65. }
  66. }

输出:

  1. 最长连续整数列表:[4, 5, 6],数量:3
  2. 最长连续整数列表:[1, 2, 3, 4, 5],数量:5
  3. 最长连续整数列表:[9, 10, 11, 12],数量:4
  4. 最长连续整数列表:[10, 11, 12],数量:3
  5. 最长连续整数列表:[7, 8, 9],数量:3
  6. 最长连续整数列表:[9],数量:1
  7. 最长连续整数列表:[1, 2],数量:2
  8. 最长连续整数列表:[1, 2, 3],数量:3
  9. 最长连续整数列表:[],数量:0

代码中有详细的注释,如有疑问或问题,请随时提问。

英文:

Do it as follows:

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.TreeSet;
  6. public class Main {
  7. public static void main(String[] args) {
  8. // Tests
  9. List&lt;Integer&gt; longestConsecutive;
  10. longestConsecutive = longestConsecutiveList(new int[] { 1, 4, 9, 5, 2, 6 });
  11. System.out.println(
  12. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  13. longestConsecutive = longestConsecutiveList(new int[] { 2, 10, 4, 1, 5, 7, 3 });
  14. System.out.println(
  15. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  16. longestConsecutive = longestConsecutiveList(new int[] { 5, 9, 7, 10, 11, 15, 12, 4, 6 });
  17. System.out.println(
  18. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  19. longestConsecutive = longestConsecutiveList(new int[] { 10, 24, 20, 30, 23, 40, 25, 10, 2, 11, 3, 12 });
  20. System.out.println(
  21. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  22. longestConsecutive = longestConsecutiveList(new int[] { 9, 7, 3, 8, 1 });
  23. System.out.println(
  24. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  25. longestConsecutive = longestConsecutiveList(new int[] { 9 });
  26. System.out.println(
  27. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  28. longestConsecutive = longestConsecutiveList(new int[] { 1, 2 });
  29. System.out.println(
  30. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  31. longestConsecutive = longestConsecutiveList(new int[] { 1, 2, 3 });
  32. System.out.println(
  33. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  34. longestConsecutive = longestConsecutiveList(null);
  35. System.out.println(
  36. &quot;Logest list of consecutive integers: &quot; + longestConsecutive + &quot;, Count: &quot; + longestConsecutive.size());
  37. }
  38. public static List&lt;Integer&gt; longestConsecutiveList(int[] a) {
  39. if (a == null) {
  40. return new ArrayList&lt;Integer&gt;();
  41. }
  42. Set&lt;Integer&gt; values = new TreeSet&lt;Integer&gt;();
  43. List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  44. List&lt;Integer&gt; tempList = new ArrayList&lt;Integer&gt;();
  45. int value = 0, temp = 0;
  46. // Add the elements of the array to the sorted set
  47. for (int i : a) {
  48. values.add(i);
  49. }
  50. // Create an iterator to navigate the sorted set
  51. Iterator&lt;Integer&gt; itr = values.iterator();
  52. // Get the first element from the sorted set, assign it to value and add it to
  53. // tempList. Since tempList has one element
  54. if (itr.hasNext()) {
  55. value = itr.next();
  56. tempList.add(value);
  57. }
  58. // Navigate the rest (2nd element onwards) of the sorted set
  59. while (itr.hasNext()) {
  60. // Get the next element from the sorted set and assign it to temp
  61. temp = itr.next();
  62. // If temp - value = 1, add temp to tempList
  63. if (temp - value == 1) {
  64. tempList.add(temp);
  65. } else if (tempList.size() &gt;= list.size()) {
  66. list = tempList;
  67. tempList = new ArrayList&lt;Integer&gt;();
  68. tempList.add(temp);
  69. } else {
  70. tempList = new ArrayList&lt;Integer&gt;();
  71. }
  72. value = temp;
  73. }
  74. return list.size() &gt; tempList.size() ? list : tempList;
  75. }
  76. }

Output:

  1. Logest list of consecutive integers: [4, 5, 6], Count: 3
  2. Logest list of consecutive integers: [1, 2, 3, 4, 5], Count: 5
  3. Logest list of consecutive integers: [9, 10, 11, 12], Count: 4
  4. Logest list of consecutive integers: [10, 11, 12], Count: 3
  5. Logest list of consecutive integers: [7, 8, 9], Count: 3
  6. Logest list of consecutive integers: [9], Count: 1
  7. Logest list of consecutive integers: [1, 2], Count: 2
  8. Logest list of consecutive integers: [1, 2, 3], Count: 3
  9. 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

  1. private static int consecutive(int[] a) {
  2. Set<Integer> values;
  3. // 用于存储连续数字的列表
  4. List<Integer> nums = new ArrayList<>();
  5. values = Arrays.stream(a).boxed().collect(Collectors.toSet());
  6. int max = 0;
  7. for (int i : values) {
  8. if (values.contains(i - 1)) {
  9. continue;
  10. }
  11. // 用于存储每个序列的内部列表
  12. List<Integer> temp = new ArrayList<>();
  13. // 将 i++ 移到循环内部,因为需要将值存储起来
  14. while (values.contains(i)) {
  15. temp.add(i);
  16. i++;
  17. }
  18. // 如果内部列表较大,则进行替换
  19. if (nums.size() <= temp.size()) {
  20. nums = temp;
  21. }
  22. max = Math.max(max, temp.size());
  23. }
  24. System.out.println(nums);
  25. return max;
  26. }
英文:
  1. private static int consecutive(int[] a) {
  2. Set&lt;Integer&gt; values;
  3. // to store the consecutive numbers
  4. List&lt;Integer&gt; nums = new ArrayList&lt;&gt;();
  5. values = Arrays.stream(a).boxed().collect(Collectors.toSet());
  6. int max = 0;
  7. for (int i : values) {
  8. if (values.contains(i - 1)) {
  9. continue;
  10. }
  11. // inner list for each sequemce
  12. List&lt;Integer&gt; temp = new ArrayList&lt;&gt;();
  13. // moved i++ inside the loop because the value is required to store
  14. while (values.contains(i)) {
  15. temp.add(i);
  16. i++;
  17. }
  18. // if the inner list is larger, replace
  19. if (nums.size() &lt;= temp.size()) {
  20. nums = temp;
  21. }
  22. max = Math.max(max, temp.size());
  23. }
  24. System.out.println(nums);
  25. return max;
  26. }

huangapple
  • 本文由 发表于 2020年4月4日 02:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61018423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定