一种检查数组中模式的递归方法

时间:2014-06-05 19:48:49

标签: java arrays

我试图在Java中编写一个递归方法,它将采用两个int数组,如果第一个数组以这种方式表示第二个数组的模式,则返回true / false - (模式数组接受0,1或2. 0表示一位或两位数字,1表示一位数字,2表示两位数字。所以如果我发送{2,3,57}和{1,0,2}它将返回true。如果我放{2,555,57}和{1,0,2}它将返回false。如果我放{2,3,573 **,4,34,35} **和{1, 0,2}我仍然需要变为现实,因为数组的一部分代表了模式。​​

我想出了这个:

private static boolean match(int [] a, int [] pattern, int i, int j, int c, int subArr)
{
    if(a.length < pattern.length)
        return false;
    else if(pattern.length == 0)
        return true;
    else if(pattern.length == a.length && check(a, pattern, i, j))
        return true;
    else if(check(a, pattern, i++, j++))
    {
        return check(a, pattern, i, j);
    }

    else return false;    
}


private static boolean check(int [] a, int [] pattern, int i, int j)
{        
    if(pattern[j] == 1 && (checkDigits(a[i]) == 1))
    {
        return true;
    }            
    else if(pattern[j] == 2 && checkDigits(a[i]) == 2)
    {
        return true;
    }
    else if(pattern[j] == 0 &&(checkDigits(a[i]) == 1 || checkDigits(a[i]) == 2 )){
        return true;
    }
    else return false;

}


private static int checkDigits(int k){
       int length = (int)(Math.log10(k)+1);
       return length;

    }

匹配方法正在进行所有检查。检查方法是检查模式并检查数字的位数。 我的问题是3位数字。如果我提出例证{2,123,54}和{1,0,2}我得到真实而不是假。我相信问题在于检查方法,但我无法找到问题。

4 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么索引'j'中数组中的元素“a”可以根据模式中索引'j'处元素处的数字具有一定数量的数字。

您可以使用for循环完成此操作;

boolean result = true;
int [...] a = ...
int [...] pattern = ...
for (int j = 0; j < a.length(); j++) {
    if (result) {
        if (pattern[j] = 0) {
            if (a[j].toString().length() > 2) {
                result = false;
            }
        } else if (pattern[j] = 1) {
            if (a[j].toString().length() != 1) {
                result = false;
            }
        } else if (pattern[j] = 2) {
            if (a[j].toString().length() != 2) {
                result = false;
            }
        }
    }
}

这应该完成工作,或类似的东西。

答案 1 :(得分:0)

检查我现在写的代码,我在代码中添加了注释,如果你运行它。我在控制台上写了一些文字,向你解释它是如何工作的。因此,当您想要使用它时,只需删除system.out.print

即可
public class ArrayPattern {
    static int numbers[] = {1,10,20,3,30};
    static int pattern[] = {0,0,2,2};

    public static void main(String[] args) {
        System.out.println(isPattern(0, 0));
    }

    /**
     * Recursive method that checks for the pattern. If it fails to match pattern starting from index i, it 
     * tries starting from index i+1
     * */
    public static boolean isPattern(int index, int consec){

        // If all pattern values where matched consecutively
        if(consec == pattern.length)
            return true;

        // If the numbers ended and the pattern wasn't found
        if(index == numbers.length)
            return false;

        // If the current number matches the pattern, check the next number at index + 1
        if(checkPattern(pattern[consec], numbers[index])){
            System.out.println(pattern[consec] +" => "+ numbers[index]);
            return isPattern(index+1, consec+1);
        }

        // If the pattern was not found, starting from a specific index. Start from the next index to check if the pattern can be found
        System.out.println(String.format("\nFailed to match pattern, try starting from index: %d\n", (index - consec + 1)));
        return isPattern(index - consec + 1, 0);
    }

     /**
 * Just chesk the pattern:
 * 0 => 1 or 2 digits. 
 * 1 => 1 digit. 
 * 2 => 2 digits 
 */
    public static boolean checkPattern(int pattern, int value){
        String sValue = String.format("%d", value);
        switch (pattern) {
            case 0:
                return sValue.length() <= 2;
            default:
                return sValue.length() == pattern;
        }
    }
}

答案 2 :(得分:0)

真正的递归解决方案可能如下:

import java.util.Arrays;

public class Checker {
//0 represent one or two digits number, 
//1 represent one digit numbers and 
//2 represent two digits numbers
public boolean match(int [] a, int [] pattern, int i, int j)
{
    if(pattern.length == 0) return true;
    if(pattern.length == a.length) {
         return check(a, pattern);
    } else {
        return false;
    }
}

    //recursive function
private  boolean check(int [] a, int [] pattern) {
    boolean firstDigitCheck = false;
    switch (pattern[0]) {
        case 0: firstDigitCheck = checkDigits(a[0]) <3;break; // 1 or 2
        case 1: firstDigitCheck = checkDigits(a[0]) <2;break; // 1
        case 2: firstDigitCheck = checkDigits(a[0]) ==2;break// 2
        default:break;//not important (we trust the pattern format)
    }       
    if (a.length==1) {//base step (array of dimension 1) 
        return firstDigitCheck;
    } else {//recursive step on the left-truncated arrays
        return firstDigitCheck && check(Arrays.copyOfRange(a, 1, a.length), Arrays.copyOfRange(pattern, 1, pattern.length));
    }
}


public int checkDigits(int k){
       int length = (int)(Math.log10(k)+1);
       return length;
}
}

答案 3 :(得分:0)

我对你的代码有点不清楚。但是可以使用for循环以更简单的方式完成。

您需要遵循的基本算法是:

j=0
for(i = 0 to a.length){
if number of digits(a[i]) == pattern[j] {
j++
}
}
 if j == pattern.length
print its a match
else
it is not.

这一小段代码适用于所有情况。

而不是for循环,只需每次都调用它。