找出给定数组中的最长连续递增序列。

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

Find the largest sequence in a given array of numbers

问题

程序用于找出给定数字列表中的最大序列。

输入:[1, 2, 3, 2, 4, 5, 6, 7, 8, 1, 0, 4, 5, 6]
期望结果:[4, 5, 6, 7, 8]

package CaseStudy;
import java.util.Arrays;

public class LargestSequence {
    public static void main(String[] args) {

        int[] array = {1, 2, 3, 2, 4, 5, 6, 7, 8, 1, 0, 4, 5, 6};
        int count = 0;
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i + 1] == (array[i]) + 1) {
                count++;
                System.out.print(array[i]);
            }
        }
    }
}

我尝试过,但无法获得所需的输出。

英文:

program to find largest sequence in a given list of numbers

INPUT = [1,2,3,2,4,5,6,7,8,1,0,4,5,6]
Expected Result: [4, 5, 6, 7, 8]

 package CaseStudy;
    import java.util.Arrays;
    
    
    
    public class LargestSequence {
    	public static void main(String[] args) {
    
    		int[] array = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};
    		int count=0;
    		for (int i = 0; i &lt; array.length-1; i++) {
    			if (array[i+1]==(array[i])+1)
    			{
    				count++;
    				System.out.print (array[i]);	
    				
    			}
    			
    	}
    		
     }
    }	

I tried and I am not able to get the desired output.

答案1

得分: 0

代码部分不需要翻译。以下是翻译好的内容:

当序列中断时,您需要将计数重置为零。以下解决方案的逻辑是,对于每个后续的连续数字,我们将计数增加1(每个新序列的计数从1开始),并且我们将当前数字附加到CSV字符串,以便在算法在完整的数字字符串上运行后稍后打印出来。

这将打印:

最长的序列是4,5,6,7,8,长度为:5

英文:

You need to reset the count to zero in the event that the sequence gets broken. The logic in the solution below is that for each subsequent in-sequence number, we increment the count by 1 (the count starts at one for each new sequence), and also we append the current digit to a CSV string, to be printed later after the algorithm has run on the full string of numbers.

int[] array = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};
StringBuilder sb = new StringBuilder(&quot;&quot; + array[0]);
String sequence = &quot;&quot;;
int count = 1;
int maxCount = 1;

for (int i=1; i &lt; array.length; i++) {
    if (array[i] == array[i-1] + 1) {
        count++;
        sb.append(&quot;,&quot;).append(array[i]);
    }
    if (array[i] != array[i-1] + 1 || i == array.length - 1) {
        if (count &gt; maxCount) {
            maxCount = count;
            sequence = sb.toString();
        }
        count = 1;
        sb = new StringBuilder(&quot;&quot; + array[i]);
    }
}

System.out.println(&quot;The longest sequence was &quot; + sequence + &quot;, with a length of: &quot; + maxCount);

<h2>Demo</h2>

This prints:

The longest sequence was 4,5,6,7,8, with a length of: 5

答案2

得分: 0

你还可以使用Java Stream来实现:

int[] array = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};

Arrays.stream(array).distinct() // 将数组转换为流并获取不重复的元素
    .boxed() // 获取整数流 Stream<Integer>
    .sorted() // 对数组进行排序
    .skip(Arrays.stream(array).distinct().count() - 5) // 跳过前面不需要的元素,保留最后5个元素。注意:对于较大的数组,这不是一个正确的做法。
    .forEach(System.out::println); // 打印每个元素

**编辑**我误解了问题您可以按以下方式获取最长的连续序列

```java
int[] a = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};
StringBuilder sb = new StringBuilder(a[0] + ""); // 第一个字符
int count = 0;

for (int i = 1; i < a.length; i++) {
    if (a[i] > a[i - 1]) {
        count++;
        sb.append(a[i]);
    } else {
        count = 0;
        sb.append(" "); // 为每个序列添加分隔符
    }
}
String largestSeq = Arrays.stream(sb.toString().split(" ")) // 根据长度获取最长序列
        .max(Comparator.comparingInt(String::length))
        .get();
英文:

You can also do with java-stream,

int[] array = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};

Arrays.stream(array).distinct(). //covert array to stream and get distinct elements
	    .boxed()                 // get Integer stream Stream&lt;Integer&gt;
	    .sorted()                // sort the array
	    .skip(Arrays.stream(array).distinct().count()-5) //last 5 elements PS. not a right way for larger arrays
	    .forEach(System.out::println);  // print each element

EDIT: I misunderstood the question. You can get largest sequence as below,

int[] a = {1,2,3,2,4,5,6,7,8,1,0,4,5,6};
StringBuilder sb = new StringBuilder(a[0]+&quot;&quot;); //first char
int count = 0;

for (int i = 1; i &lt; a.length; i++) {
    if (a[i] &gt; a[i - 1]) {
        count++;
        sb.append(a[i]);
    } else {
        count = 0;
        sb.append(&quot; &quot;);    // add spearator for each sequesnce
    }
}
String largestSeq = Arrays.stream(sb.toString().split(&quot; &quot;)) //get the largese seq based on lenght
        .max(Comparator.comparingInt(String::length))
        .get();

huangapple
  • 本文由 发表于 2020年4月10日 17:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61137603.html
匿名

发表评论

匿名网友

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

确定