题解 | #24点游戏算法#
24点游戏算法
http://www.nowcoder.com/practice/36a06357855c4cd191f9d5ac8e75fec5
import java.util.*;
public class Solution {
public boolean ans = false;
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param nums int整型ArrayList
* @return bool布尔型
*/
public boolean Point24(ArrayList<Integer> nums) {
// write code here
process(0.0, nums);
return ans;
}
public void process(double previousTotal, ArrayList<Integer> remainNums) {
if (0 == remainNums.size()) {
if (previousTotal == (double) 24) {
ans = true;
}
return;
}
for (int currentNumber : remainNums) {
ArrayList<Integer> copyArr = new ArrayList<>(remainNums);
copyArr.remove(Integer.valueOf(currentNumber));
process(previousTotal + currentNumber, copyArr);
process(previousTotal - currentNumber, copyArr);
process(previousTotal * currentNumber, copyArr);
process(previousTotal / currentNumber, copyArr);
}
}
}