递归以查找两个整数之间的公共数字的最后一个索引

时间:2018-04-21 11:25:19

标签: java recursion integer

我正在尝试实现一个方法,找到两个int之间的最高公共数字索引,如下所示:   a = 2 2 24,b = 4 2 22结果将是index = 2,这意味着索引是反向的。我设法找到第一个常见数字,但我找不到最后一个常见数字。这是找到第一个公共数字的方法:

private static int getLowestIndexWithSameDigit(int a, int b) {
    if (a < 0 || b < 0)
        throw new IllegalArgumentException("Ambos os argumentos devem ser positivos: " + a + " " + b);

    else {
        if (a % 10 == b % 10) {
            return indice;
        } else if (a / 10 != 0 && b / 10 != 0) {
            indice++;
            return getLowestIndexWithSameDigit(a / 10, b / 10);
        } else {
            return -1;
        }
    }
}

每次使用getLowestIndexWithSameDigit时将索引初始化为0的方法:

private static void test_getLowestIndexWithSameDigit(int a, int b) {
    try {

        System.out.print("getLowestIndexWithSameDigit (" + a + ", " + b + ") = ");
        indice = 0;
        int res = getLowestIndexWithSameDigit(a, b);
        System.out.println(res);

    } catch (IllegalArgumentException e) {
        System.out.println("Erro: " + e.getMessage());
    }
}

我试图以某种方式调整这种方法,但我认为它不适合找到最后一个常见数字。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

login(email, password): Observable<string> { const url = Utils.baseBackendUrl + '/login'; const headers = new HttpHeaders({'Content-Type': 'application/json'}); this.http.post<any>(url, JSON.stringify({'user_email': email, 'user_password': password, 'locale': 'cs_CZ'}), {headers: headers, observe: 'response', responseType: 'text' as 'json'}) .subscribe(resp => { console.log(resp.headers.get('successful')); console.log(resp.headers.get('message')); }); return of('ahoj'); } 转换为int s:

可能更简单
String

//you can overload method to support other primitives private static int getHighestIndexWithSameDigit(int a, int b) { String aS = String.valueOf(a); String bS = String.valueOf(b); return getHighestIndexWithSameDigit(aS, bS); } //helper method to set strat index private static int getHighestIndexWithSameDigit(String aS, String bS) { return getHighestIndexWithSameDigit(aS, bS, 1); } //recursively check first letter. First index is 1 - indicates first char from left private static int getHighestIndexWithSameDigit(String aS, String bS, int index) { int aLength = aS.length(), bLength = bS.length(); if((aLength == 0) || (bLength == 0)) { return -1;} if(aS.charAt(0) == bS.charAt(0)) { return index; } //remove first letters, update index return getHighestIndexWithSameDigit(aS.substring(1, aLength), bS.substring(1, bLength), ++index); }

进行测试

编辑:
发布的代码假设第一个索引是1(1个基数),其中值1的索引表示从左开始的第一个数字。
如果你打算使用0基本索引,那么从右边两个方法计算应该略有改变:

System.out.println(getHighestIndexWithSameDigit(2224 , 4222) );

答案 1 :(得分:0)

获得最低或最高应该只是你停止循环的问题,不是吗? 下面代码的略微重写版本至少对我有用。

private static int getDatIndex(int a, int b, boolean getDatLow) {
    int indice = -1;
    int index = 0;

    while (a/10 != 0 && b/10 != 0) {
        if (a % 10 == b % 10) {
            indice = index;
            // If you want the lowest common digit index: stop the loop.
            if (getDatLow) {
                break;
            }
        }

        a = a/10;
        b = b/10;
        index++;
    }

    return indice;
}