【问题标题】:Increasing Subsequence Recursive Java增加子序列递归Java
【发布时间】:2019-05-13 05:33:37
【问题描述】:

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

  • 根本没有循环,只有递归
  • 没有列表和导入(所以没有地图左右)&?
  • 不改变函数increasing(int[] x, int length)的签名
  • 您可以添加私有函数,但不能添加整数/布尔值等。

我想过用一个老问题,最长递增子序列,然后比较大小,如果给定的大小比 LIS 大,它会返回 false。但是,我的 LIS 代码似乎缺少跳过数字并重复数字的情况,例如 9,7,5,4,7,1,-3,8 为 3 返回 false 而不是 true,也为 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;
}

【问题讨论】:

  • 你的作业要求使用递归吗?
  • 是的,我只能使用递归
  • 请缩进你的代码
  • 你能解释一下为什么9,7,5,4,7,1,-3,8 for 3 应该返回true吗?我只看到有 2 个数字的序列4, 7
  • @statut 因为它包含 {5,7,8}

标签: java recursion subsequence


【解决方案1】:

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

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);
}

当您必须考虑非连续项目的序列时,它会变得更加复杂。在这种情况下,当您遇到小于其前身的元素时,您无需立即返回false,而是只需在不增加深度的情况下向下移动调用堆栈,并在比较最后两项时跟踪要跳过的元素数量序列。 (注意,这需要额外检查以防止递归超出数组大小):

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

private static boolean increasing(int[] x, int length, int depth, int skip) {
    if (x.length < length) return false;
    if (depth >= length) return true;
    if (depth + skip >= x.length) return false;

    if (depth > 0 && x[depth - 1] > x[depth + skip]) {
        return increasing(x, length, depth, skip + 1);
    }

    return increasing(x, length, depth + 1, 0);
}

【讨论】:

  • 谢谢,但是答案不处理未排序的数组,当输入 {9,7,5,4,7,1,-3,8}, 2 或 3 它返回 false 因为它没有正确跳过数字
【解决方案2】:
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
}

【讨论】:

  • 谢谢你也检查,但是我们不允许使用? (如上所述),我怎样才能将其翻译成 if-else?
猜你喜欢
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2017-07-10
  • 2013-07-03
相关资源
最近更新 更多