[英]How can I find the biggest string of numbers?
There are array elements eg: 2 3 4 7 8. I need to write to console what is the biggest string of numbers, so the solution will be 2-4, because 2 3 4 --> 4-2=2 is bigger than 7 8 -> 8-7=1有数组元素,例如:2 3 4 7 8。我需要向控制台写入最大的数字字符串,所以解决方案将是 2-4,因为 2 3 4 --> 4-2=2 大于7 8 -> 8-7=1
I need to find the longest growing row, which increases by 1. 2 3 4 (2+1=3, 3+1=4 and 4+1= 7 it's wrong. And in 2 3 4 row has 2 element and it's the longest not 7 8 where it is just 1我需要找到最长的增长行,增加 1. 2 3 4 (2+1=3, 3+1=4 and 4+1= 7 这是错误的。在 2 3 4 行中有 2 个元素,它是最长的不是 7 8,它只是 1
int first=0;
int last=0;
for(int i=0; i < n; i++)
{
if(t[i]-t[i-1]==1)
{
first=t[i];
}
else
{
last=t[i];
}
}
With this code the solution is 7 8, so the code will the last pair (7 8).使用此代码,解决方案是 7 8,因此代码将是最后一对 (7 8)。
The following function takes an int array and size as input, as in your example code.以下 function 将 int 数组和大小作为输入,如您的示例代码中所示。 And return the maximum increasing size.
并返回最大增加的大小。 It may not be what you are looking for exactly, but your question is not very clear.
它可能不是您正在寻找的确切内容,但您的问题不是很清楚。
int func(int[] arr, int size) {
// trivial case
if (size == 0)
return 1;
int max = 1; // At least 1 when there is an element
int cur = 1; // At least 1 when there is an element
int first;
int second;
int cur_first;
cur_first = arr[0];
for (int i = 0; i < size-1; i++) {
if (arr[i] == arr[i+1] -1) {
cur ++;
}
else {
if (cur > max) {
max = cur;
first = cur_first;
second = arr[i];
}
cur = 1;
cur_first = arr[i+1];
}
}
if (cur > max) {
cur = max;
first = cur_first;
second = arr[size-1];
}
return max
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.