Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递增子序列递归Java_Java_Recursion_Subsequence - Fatal编程技术网

递增子序列递归Java

递增子序列递归Java,java,recursion,subsequence,Java,Recursion,Subsequence,我有以下问题:如果每一个数都是单调递增的,那么一个数字序列就是单调递增的 序列中的数字大于或等于其前面的数字。编写一个布尔函数递增(int[]x,int length),如果给定数组包含给定长度的递增子序列,则返回true,否则返回false。准则: 根本没有循环,只有递归 没有列表和导入(因此没有映射或导入)&? 不更改函数的签名增加(int[]x,int-length) 您可以添加私有函数,但不能添加int/booleans等 我考虑使用一个老问题,最长递增子序列,然后比较大小,如果给定

我有以下问题:如果每一个数都是单调递增的,那么一个数字序列就是单调递增的 序列中的数字大于或等于其前面的数字。编写一个布尔函数
递增(int[]x,int length)
,如果给定数组包含给定长度的递增子序列,则返回true,否则返回false。准则:

  • 根本没有循环,只有递归
  • 没有列表和导入(因此没有映射或导入)&
  • 不更改函数的签名
    增加(int[]x,int-length)
  • 您可以添加私有函数,但不能添加int/booleans等
我考虑使用一个老问题,最长递增子序列,然后比较大小,如果给定的大小大于LIS,它将返回false。然而,我的LIS代码似乎缺少跳过一个数字并重复一个数字的情况,例如
9,7,5,4,7,1,-3,8
为3而不是为真返回false,同样为
3,1,1,2
返回false

public static boolean increasing(int[] x, int length) {
    int i = 0;
    int ans = longestIncreasing(x, length, i);
    return (ans >= length);
}

private static int longestIncreasing(int[] arr, int n, int i) {
    if (n == 0) {
        return 1;
    }

    int m = 1, temp;
    if (arr[i++] < arr[n--]) {
        temp = 1 + longestIncreasing(arr, n, i);
        if (temp > m) {
            m = temp;    //   m = max(m, 1 + _lis(arr, i));
        }
    }
    else {
        longestIncreasing(arr, n--, i++);
    }
    return m;
}
公共静态布尔递增(int[]x,int-length){
int i=0;
int ans=长增量(x,长度,i);
返回(ans>=长度);
}
私有静态int longestincreating(int[]arr,int n,int i){
如果(n==0){
返回1;
}
int m=1,温度;
如果(arr[i++]米){
m=temp;//m=max(m,1+_-lis(arr,i));
}
}
否则{
长期递增(arr,n--,i++);
}
返回m;
}

在这种情况下,找到最长的递增序列似乎是一个更难解决的问题。查找特定长度的连续序列的问题只需要在递归调用堆栈的每一级向索引变量添加一个,并与目标长度进行比较。因此,在简单的情况下,您的问题可以这样解决:

public static boolean increasing(int[] x, int length) {
    return increasing(x, length, 0);
}

private static boolean increasing(int[] x, int length, int depth) {
    if (x.length < length) return false;
    if (depth >= length) return true;
    if (depth > 0 && x[depth - 1] > x[depth]) return false;

    return increasing(x, length, depth + 1);
}
演示:

public static void main(String... args) {
    System.out.println(increasing(new int[] { 3, 1, 1, 2 }, 3));    // true
    System.out.println(increasing(new int[] { 9, 7, 5, 4, 7, 1, -3, 8 }, 3));   // true
}

你的作业要求使用递归吗?是的,我只能使用递归。请缩进你的代码。你能解释为什么3的
9,7,5,4,7,1,-3,8
应该返回
true
?我只看到带有2个数字的序列
4,7
@statut,因为它包含{5,7,8}谢谢你,但是答案不处理未排序的数组,当输入{9,7,5,4,7,1,-3,8},2或3时,它返回false,因为它没有正确跳过数字也需要你检查,但是我们不允许使用?(如上所列),我如何将其转换为if-else?
public static boolean increasing(int[] x, int length) {
    return increasing(x, length, x[0], 0, 0) >= length;
}

private static int increasing(int[] x, int length, int min, int i, int from) {
    if (i >= x.length)
        return 0;    

    int res = increasing(x, length, Math.max(min, x[i]), i + 1, from);

    if (x[i] >= min)
        res++;    
    if (i != from || res >= length || i + length > x.length)
        return res;
    return increasing(x, length, x[i + 1], i + 1, i + 1);
}
public static void main(String... args) {
    System.out.println(increasing(new int[] { 3, 1, 1, 2 }, 3));    // true
    System.out.println(increasing(new int[] { 9, 7, 5, 4, 7, 1, -3, 8 }, 3));   // true
}