Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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_Algorithm_Backtracking - Fatal编程技术网

Java 二进制表位码

Java 二进制表位码,java,algorithm,backtracking,Java,Algorithm,Backtracking,我试图用回溯法解决LeetCode中的问题 我的代码在下面(不工作) C++代码输出: [3:00, 5:00, 9:00, 1:01, 1:02, 1:04, 1:08, 1:16, 1:32, 6:00, 10:00, 2:01, 2:02, 2:04, 2:08, 2:16, 2:32, 4:01, 4:02, 4:04, 4:08, 4:16, 4:32, 8:01, 8:02, 8:04, 8:08, 8:16, 8:32, 0:03, 0:05, 0:09, 0:17, 0:33,

我试图用回溯法解决LeetCode中的问题

我的代码在下面(不工作)

C++代码输出:

[3:00, 5:00, 9:00, 1:01, 1:02, 1:04, 1:08, 1:16, 1:32, 6:00, 10:00, 2:01, 2:02, 2:04, 2:08, 2:16, 2:32, 4:01, 4:02, 4:04, 4:08, 4:16, 4:32, 8:01, 8:02, 8:04, 8:08, 8:16, 8:32, 0:03, 0:05, 0:09, 0:17, 0:33, 0:06, 0:10, 0:18, 0:34, 0:12, 0:20, 0:36, 0:24, 0:40, 0:48]
num=2的我的呼叫跟踪

2 - [1, 2, 4, 8] - [1, 2, 4, 8, 16, 32]
  1 - [2, 4, 8] - [1, 2, 4, 8, 16, 32]
    0 - [4, 8] - [1, 2, 4, 8, 16, 32]
    0 - [2, 8] - [1, 2, 4, 8, 16, 32]
    0 - [2, 4] - [1, 2, 4, 8, 16, 32]
    0 - [2, 4, 8] - [2, 4, 8, 16, 32]
    0 - [2, 4, 8] - [1, 4, 8, 16, 32]
    0 - [2, 4, 8] - [1, 2, 8, 16, 32]
    0 - [2, 4, 8] - [1, 2, 4, 16, 32]
    0 - [2, 4, 8] - [1, 2, 4, 8, 32]
    0 - [2, 4, 8] - [1, 2, 4, 8, 16]
  1 - [1, 4, 8] - [1, 2, 4, 8, 16, 32]
    0 - [4, 8] - [1, 2, 4, 8, 16, 32]
    0 - [1, 8] - [1, 2, 4, 8, 16, 32]
    0 - [1, 4] - [1, 2, 4, 8, 16, 32]
    0 - [1, 4, 8] - [2, 4, 8, 16, 32]
    0 - [1, 4, 8] - [1, 4, 8, 16, 32]
    0 - [1, 4, 8] - [1, 2, 8, 16, 32]
    0 - [1, 4, 8] - [1, 2, 4, 16, 32]
    0 - [1, 4, 8] - [1, 2, 4, 8, 32]
    0 - [1, 4, 8] - [1, 2, 4, 8, 16]
  1 - [1, 2, 8] - [1, 2, 4, 8, 16, 32]
    0 - [2, 8] - [1, 2, 4, 8, 16, 32]
    0 - [1, 8] - [1, 2, 4, 8, 16, 32]
    0 - [1, 2, 8] - [2, 4, 8, 16, 32]
    0 - [1, 2, 8] - [1, 4, 8, 16, 32]
    0 - [1, 2, 8] - [1, 2, 8, 16, 32]
    0 - [1, 2, 8] - [1, 2, 4, 16, 32]
    0 - [1, 2, 8] - [1, 2, 4, 8, 32]
    0 - [1, 2, 8] - [1, 2, 4, 8, 16]
  1 - [1, 2, 4] - [1, 2, 4, 8, 16, 32]
    0 - [2, 4] - [1, 2, 4, 8, 16, 32]
    0 - [1, 4] - [1, 2, 4, 8, 16, 32]
    0 - [1, 2, 4] - [2, 4, 8, 16, 32]
    0 - [1, 2, 4] - [1, 4, 8, 16, 32]
    0 - [1, 2, 4] - [1, 2, 8, 16, 32]
    0 - [1, 2, 4] - [1, 2, 4, 16, 32]
    0 - [1, 2, 4] - [1, 2, 4, 8, 32]
    0 - [1, 2, 4] - [1, 2, 4, 8, 16]
  1 - [1, 2, 4, 8] - [2, 4, 8, 16, 32]
    0 - [2, 4, 8] - [2, 4, 8, 16, 32]
    0 - [1, 4, 8] - [2, 4, 8, 16, 32]
    0 - [1, 2, 8] - [2, 4, 8, 16, 32]
    0 - [1, 2, 4] - [2, 4, 8, 16, 32]
    0 - [1, 2, 4, 8] - [4, 8, 16, 32]
    0 - [1, 2, 4, 8] - [2, 8, 16, 32]
    0 - [1, 2, 4, 8] - [2, 4, 16, 32]
    0 - [1, 2, 4, 8] - [2, 4, 8, 32]
    0 - [1, 2, 4, 8] - [2, 4, 8, 16]
  1 - [1, 2, 4, 8] - [1, 4, 8, 16, 32]
    0 - [2, 4, 8] - [1, 4, 8, 16, 32]
    0 - [1, 4, 8] - [1, 4, 8, 16, 32]
    0 - [1, 2, 8] - [1, 4, 8, 16, 32]
    0 - [1, 2, 4] - [1, 4, 8, 16, 32]
    0 - [1, 2, 4, 8] - [4, 8, 16, 32]
    0 - [1, 2, 4, 8] - [1, 8, 16, 32]
    0 - [1, 2, 4, 8] - [1, 4, 16, 32]
    0 - [1, 2, 4, 8] - [1, 4, 8, 32]
    0 - [1, 2, 4, 8] - [1, 4, 8, 16]
  1 - [1, 2, 4, 8] - [1, 2, 8, 16, 32]
    0 - [2, 4, 8] - [1, 2, 8, 16, 32]
    0 - [1, 4, 8] - [1, 2, 8, 16, 32]
    0 - [1, 2, 8] - [1, 2, 8, 16, 32]
    0 - [1, 2, 4] - [1, 2, 8, 16, 32]
    0 - [1, 2, 4, 8] - [2, 8, 16, 32]
    0 - [1, 2, 4, 8] - [1, 8, 16, 32]
    0 - [1, 2, 4, 8] - [1, 2, 16, 32]
    0 - [1, 2, 4, 8] - [1, 2, 8, 32]
    0 - [1, 2, 4, 8] - [1, 2, 8, 16]
  1 - [1, 2, 4, 8] - [1, 2, 4, 16, 32]
    0 - [2, 4, 8] - [1, 2, 4, 16, 32]
    0 - [1, 4, 8] - [1, 2, 4, 16, 32]
    0 - [1, 2, 8] - [1, 2, 4, 16, 32]
    0 - [1, 2, 4] - [1, 2, 4, 16, 32]
    0 - [1, 2, 4, 8] - [2, 4, 16, 32]
    0 - [1, 2, 4, 8] - [1, 4, 16, 32]
    0 - [1, 2, 4, 8] - [1, 2, 16, 32]
    0 - [1, 2, 4, 8] - [1, 2, 4, 32]
    0 - [1, 2, 4, 8] - [1, 2, 4, 16]
  1 - [1, 2, 4, 8] - [1, 2, 4, 8, 32]
    0 - [2, 4, 8] - [1, 2, 4, 8, 32]
    0 - [1, 4, 8] - [1, 2, 4, 8, 32]
    0 - [1, 2, 8] - [1, 2, 4, 8, 32]
    0 - [1, 2, 4] - [1, 2, 4, 8, 32]
    0 - [1, 2, 4, 8] - [2, 4, 8, 32]
    0 - [1, 2, 4, 8] - [1, 4, 8, 32]
    0 - [1, 2, 4, 8] - [1, 2, 8, 32]
    0 - [1, 2, 4, 8] - [1, 2, 4, 32]
    0 - [1, 2, 4, 8] - [1, 2, 4, 8]
  1 - [1, 2, 4, 8] - [1, 2, 4, 8, 16]
    0 - [2, 4, 8] - [1, 2, 4, 8, 16]
    0 - [1, 4, 8] - [1, 2, 4, 8, 16]
    0 - [1, 2, 8] - [1, 2, 4, 8, 16]
    0 - [1, 2, 4] - [1, 2, 4, 8, 16]
    0 - [1, 2, 4, 8] - [2, 4, 8, 16]
    0 - [1, 2, 4, 8] - [1, 4, 8, 16]
    0 - [1, 2, 4, 8] - [1, 2, 8, 16]
    0 - [1, 2, 4, 8] - [1, 2, 4, 16]
    0 - [1, 2, 4, 8] - [1, 2, 4, 8]
C++代码调用跟踪:

2 - [1, 2, 4, 8] - [1, 2, 4, 8, 16, 32]
  1 - [2, 4, 8] - [1, 2, 4, 8, 16, 32]
  0 - [4, 8] - [1, 2, 4, 8, 16, 32]
  0 - [8] - [1, 2, 4, 8, 16, 32]
  0 - [] - [1, 2, 4, 8, 16, 32]
  0 - [] - [2, 4, 8, 16, 32]
  0 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [4, 8] - [1, 2, 4, 8, 16, 32]
  0 - [8] - [1, 2, 4, 8, 16, 32]
  0 - [] - [1, 2, 4, 8, 16, 32]
  0 - [] - [2, 4, 8, 16, 32]
  0 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [8] - [1, 2, 4, 8, 16, 32]
  0 - [] - [2, 4, 8, 16, 32]
  0 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [1, 2, 4, 8, 16, 32]
  0 - [] - [2, 4, 8, 16, 32]
  0 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [2, 4, 8, 16, 32]
  0 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [4, 8, 16, 32]
  0 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [8, 16, 32]
  0 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [16, 32]
  0 - [] - [32]
  0 - [] - []
  1 - [] - [32]
  0 - [] - []
  1 - [] - []

谢谢。

解决此问题的回溯方法是多余的。您可以使用一些位操作技术来解决这个问题

观察点

用于表示小时和分钟的设置位总数应等于传递的参数
n

然后你有
0-11
小时和
0-59
分钟。使用两个for循环,只选择小时
h
和minute
m
其中设置位的总和等于
n
的小时,您可以使解决方案更加优雅和快速。这个问题的目的也是为了测试你的比特操作技能

    public List<String> readBinaryWatch(int n) {
        List<String> ret = new ArrayList<>();
        for(int h = 0; h < 12; h++) {
            for(int m = 0; m < 60; m++) {
                if(Integer.bitCount(h) + Integer.bitCount(m) == n) { // Checking whether the current h and m combination is a valid result. 
                   //If the sum of their set bits is equal to n then the combination is a valid result and add that combination to result
                    ret.add(String.format("%d:%02d",h,m));
                }
            }
        }
        return ret;
    }
公共列表readBinaryWatch(int n){
List ret=new ArrayList();
对于(int h=0;h<12;h++){
对于(int m=0;m<60;m++){
if(Integer.bitCount(h)+Integer.bitCount(m)==n){//检查当前的h和m组合是否为有效结果。
//如果它们的设置位之和等于n,则该组合为有效结果,并将该组合添加到结果中
ret.add(String.format(“%d:%02d”,h,m));
}
}
}
返回ret;
}

我同意上面的答案,我也这样解决了这个问题

我也有一些不同的解决方案。你可以在这里找到它

其思想是,您可以为每个小时计算小时组合。例如,0个led表示0小时,1个led表示1、2、4、8中的任何选项。 更多细节

分钟也是如此。一分钟led表示1、2、4、8、16、32分钟内的任何选项

现在,您有了手表上指示灯的数量<代码>nLeds=小时数+分钟数。在这种情况下,您将拥有所有可能的小时组合和分钟组合,您只需加入它们

现在,从中可以找到具有定义值的表。您可以硬编码这些表,也可以自己构建它们

// Hours combinations depending on leds number
    private static final int[] hours0 = calculateLedsNumbers(0, 12);
    private static final int[] hours1 = calculateLedsNumbers(1, 12);
    private static final int[] hours2 = calculateLedsNumbers(2, 12);
    private static final int[] hours3 = calculateLedsNumbers(3, 12);
    private static final int[] hours4 = calculateLedsNumbers(4, 12);

    // Minutes combinations depending on leds number
    private static final int[] minutes0 = calculateLedsNumbers(0, 60);
    private static final int[] minutes1 = calculateLedsNumbers(1, 60);
    private static final int[] minutes2 = calculateLedsNumbers(2, 60);
    private static final int[] minutes3 = calculateLedsNumbers(3, 60);
    private static final int[] minutes4 = calculateLedsNumbers(4, 60);
    private static final int[] minutes5 = calculateLedsNumbers(5, 60);
    private static final int[] minutes6 = calculateLedsNumbers(6, 60);
然后你循环所有可能的小时和分钟的组合,并将它们组合起来

final List<String> result = new ArrayList<>(191);
    for (int hoursLeds = 0; hoursLeds < 4; hoursLeds++) {
        int[] possibleHours = getHoursFromLed(hoursLeds);
        for (int minutesLeds = 0; minutesLeds < 6; minutesLeds++) {
            if (hoursLeds + minutesLeds > nLeds) {
                break;
            }

            if (hoursLeds + minutesLeds == nLeds) {
                int [] possibleMinutes = getMinutesFromLed(minutesLeds);
                combineHoursAndMinutes(possibleHours, possibleMinutes, result);
            }
        }
    }
最终列表结果=新的ArrayList(191);
对于(int hoursLeds=0;hoursLeds<4;hoursLeds++){
int[]possibleHours=getHoursFromLed(hoursLeds);
对于(int-minutesLeds=0;minutesLeds<6;minutesLeds++){
如果(小时发光二极管+分钟发光二极管>nLeds){
打破
}
如果(小时数+分钟数==nLeds){
int[]possibleMinutes=getMinutesFromLed(MinutesLED);
结合小时和分钟(可能的小时、可能的分钟、结果);
}
}
}
并结合

private static void combineHoursAndMinutes(int[] possibleHours, int[] possibleMinutes, List<String> result) {
    for (int possibleHour : possibleHours) {
        for (int possibleMinute : possibleMinutes) {
              // Same as String.format("%d:%02d", possibleHour, possibleMinute), but faster.
            result.add(hoursFormatted[possibleHour] + minutesFormatted[possibleMinute]);
        }
    }
}
私有静态无效组合小时和分钟(int[]可能小时,int[]可能小时,列表结果){
for(int-possibleHours:possibleHours){
for(int可能数:可能数){
//与String.format相同(“%d:%02d”,可能为小时,可能为分钟),但速度更快。
结果.add(小时格式化[可能小时]+分钟格式化[可能分钟]);
}
}
}
顺便说一下,这就是结果

您能否在问题中添加更多关于代码中哪些不起作用的信息?只有11个可能的输入,所以其中一个肯定是错误的。您的代码是否错过了某个特定输出的一些可能时间?它会崩溃吗?还有什么吗?嘿,Paul,添加了示例输出和调用跟踪。我认为java中的
通过值和引用调用
会造成这个问题。检查这个
// Hours combinations depending on leds number
    private static final int[] hours0 = calculateLedsNumbers(0, 12);
    private static final int[] hours1 = calculateLedsNumbers(1, 12);
    private static final int[] hours2 = calculateLedsNumbers(2, 12);
    private static final int[] hours3 = calculateLedsNumbers(3, 12);
    private static final int[] hours4 = calculateLedsNumbers(4, 12);

    // Minutes combinations depending on leds number
    private static final int[] minutes0 = calculateLedsNumbers(0, 60);
    private static final int[] minutes1 = calculateLedsNumbers(1, 60);
    private static final int[] minutes2 = calculateLedsNumbers(2, 60);
    private static final int[] minutes3 = calculateLedsNumbers(3, 60);
    private static final int[] minutes4 = calculateLedsNumbers(4, 60);
    private static final int[] minutes5 = calculateLedsNumbers(5, 60);
    private static final int[] minutes6 = calculateLedsNumbers(6, 60);
final List<String> result = new ArrayList<>(191);
    for (int hoursLeds = 0; hoursLeds < 4; hoursLeds++) {
        int[] possibleHours = getHoursFromLed(hoursLeds);
        for (int minutesLeds = 0; minutesLeds < 6; minutesLeds++) {
            if (hoursLeds + minutesLeds > nLeds) {
                break;
            }

            if (hoursLeds + minutesLeds == nLeds) {
                int [] possibleMinutes = getMinutesFromLed(minutesLeds);
                combineHoursAndMinutes(possibleHours, possibleMinutes, result);
            }
        }
    }
private static void combineHoursAndMinutes(int[] possibleHours, int[] possibleMinutes, List<String> result) {
    for (int possibleHour : possibleHours) {
        for (int possibleMinute : possibleMinutes) {
              // Same as String.format("%d:%02d", possibleHour, possibleMinute), but faster.
            result.add(hoursFormatted[possibleHour] + minutesFormatted[possibleMinute]);
        }
    }
}