java - 使用智能算法缩短 switch case

标签 java algorithm

我正在寻找一种智能算法来获得(或多或少)与以下方法相同的结果。

返回哪个数字实际上并不重要。重要的是,每个低/高组合都有一个唯一的返回值,返回值在 1 到 36 之间。

idx 介于 0 和 8(9 个值)之间,从不低于,从不高于。

有什么想法吗?

// idx1 is never = idx2
private long getIdentifier(int idx1, int idx2) {
    int low = (idx1>idx2)?idx2:idx1;
    int high = ((idx1>idx2)?idx1:idx2);
    switch (low) {
        case 0:
            return high;
        case 1:
            switch (high) {
                case 2:
                    return 9;
                case 3:
                    return 10;
                case 4:
                    return 11;
                case 5:
                    return 12;
                case 6:
                    return 13;
                case 7:
                    return 14;
                case 8:
                    return 15;
            }
        case 2:
            switch (high) {
                case 3:
                    return 16;
                case 4:
                    return 17;
                case 5:
                    return 18;
                case 6:
                    return 19;
                case 7:
                    return 20;
                case 8:
                    return 21;
            }
        case 3:
            switch (high) {
                case 4:
                    return 22;
                case 5:
                    return 23;
                case 6:
                    return 24;
                case 7:
                    return 25;
                case 8:
                    return 26;
            }
        case 4:
            switch (high) {
                case 5:
                    return 27;
                case 6:
                    return 28;
                case 7:
                    return 29;
                case 8:
                    return 30;
            }
        case 5:
            switch (high) {
                case 6:
                    return 31;
                case 7:
                    return 32;
                case 8:
                    return 33;
            }
        case 6:
            switch (high) {
                case 7:
                    return 34;
                case 8:
                    return 35;
            }
        case 7:
            return 36;
    }
    return 0;
}

最佳答案

您可以将返回值存储在二维数组中。

int[] res = new int[9][9];
...
res[3][8] = 26;
...
return res[low][high];

您可以使用单独的赋值(例如 res[3][8]=26)初始化数组,也可以在一行中初始化数组:

int[] res = {{0,1,2,3,4,5,6,7,8},{...},{...},{...},{...},{...},{...},{...},{...}};

您可以轻松修改二维数组以仅包含您实际使用的位置:

int[] res = {{0,1,2,3,4,5,6,7,8},
             {9,10,11,12,13,14,15},
             {16,17,18,19,20,21},
             {22,23,24,25,26},
             {27,28,29,30},
             {31,32,33},
             {34,35},
             {36}};

现在您需要在访问数组之前测试以下条件:

if (low < high && low < res.length && (high - low - 1) < res[low].length) {
    return res[low][high-low-1];
} else {
    return 0;
}

关于java - 使用智能算法缩短 switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805372/

相关文章:

java - Tomcat 上的 Web 应用程序

java - @ExceptionHandler for type Exception not called for Spring exception in custom ResponseEntityExceptionHandler

algorithm - 如何找到二维数组中的最大圆

c# - 为什么下面的代码是错误的?二项式系数

r - 从图像中提取多边形

java - Lombok 中带有@Builder 或@Getter 注释的默认字段值

java - Junit 零测试

java - 如何配置测试hbase程序

algorithm - 对 Clojure 中功能性的广度优先树遍历感到困惑?

algorithm - 树遍历以层次优先顺序和深度优先顺序递归