原题目:https://leetcode-cn.com/problems/24-game/
思路:
采用枚举的思路,第一次去两个数进行四则运算12*4,第二次取取个数进行四则运算6*4,第三次取两个数进行运算2*4
注意:减法和除法 依赖于 操作顺序
代码:
class Solution {
public:
bool judgePoint24(vector<int>& nums) {
vector<double> num1;
//转换成实数类型
for(int i=0;i<nums.size();i++) num1.push_back(double(nums[i]));
return solve(num1);
}
bool solve(vector<double> nums){
if(nums.size()==0) return false;
if(nums.size()==1) return abs(nums[0]-24) <1e-3;
//抽取两个数字进行运算
for(int i=0;i<nums.size();i++){
for(int j=0;j<nums.size();j++){
if(i!=j){
//把没有运算的操作符,存入到新的数组
vector<double> nums2;
for(int k=0;k<nums.size();k++){
if(k!=i && k!=j) nums2.push_back(nums[k]);
}
//依次进行四种操作
for(int k=0;k<4;k++){
//+ *只进行一次,因为操作顺序不改变结果
if(k<2&&i>j) continue;
if(k==0) nums2.push_back(nums[i]+nums[j]);
if(k==1) nums2.push_back(nums[i]*nums[j]);
if(k==2) nums2.push_back(nums[i]-nums[j]);
if(k==3){
if(nums[j]!=0) nums2.push_back(nums[i]/nums[j]);
else continue;
}
if(solve(nums2)) return true;
nums2.pop_back();
}
}
}
}
return false;
}
};
或者直接枚举,代码:
class Solution {
public:
bool judgePoint24(vector<int>& nums) {
double a = nums[0], b = nums[1], c = nums[2], d = nums[3];
return judgePoint24_4(a, b, c, d);
}
bool judgePoint24_1(double a) {
return abs(a - 24) < 1e-6;
}
bool judgePoint24_2(double a, double& b) {
return
judgePoint24_1(a + b) ||
judgePoint24_1(a - b) ||
judgePoint24_1(b - a) ||
judgePoint24_1(a * b) ||
judgePoint24_1(a / b) ||
judgePoint24_1(b / a);
}
bool judgePoint24_3(double a, double& b, double& c) {
return
judgePoint24_2(b + c, a) ||
judgePoint24_2(b - c, a) ||
judgePoint24_2(c - b, a) ||
judgePoint24_2(b * c, a) ||
judgePoint24_2(b / c, a) ||
judgePoint24_2(c / b, a) ||
judgePoint24_2(a + c, b) ||
judgePoint24_2(a - c, b) ||
judgePoint24_2(c - a, b) ||
judgePoint24_2(a * c, b) ||
judgePoint24_2(a / c, b) ||
judgePoint24_2(c / a, b) ||
judgePoint24_2(a + b, c) ||
judgePoint24_2(a - b, c) ||
judgePoint24_2(b - a, c) ||
judgePoint24_2(a * b, c) ||
judgePoint24_2(a / b, c) ||
judgePoint24_2(b / a, c);
}
bool judgePoint24_4(double& a, double& b, double& c, double& d) {
return
judgePoint24_3(c + d, a, b) ||
judgePoint24_3(c - d, a, b) ||
judgePoint24_3(d - c, a, b) ||
judgePoint24_3(d * c, a, b) ||
judgePoint24_3(c / d, a, b) ||
judgePoint24_3(d / c, a, b) ||
judgePoint24_3(b + d, a, c) ||
judgePoint24_3(b - d, a, c) ||
judgePoint24_3(d - b, a, c) ||
judgePoint24_3(b * d, a, c) ||
judgePoint24_3(b / d, a, c) ||
judgePoint24_3(d / b, a, c) ||
judgePoint24_3(b + c, a, d) ||
judgePoint24_3(b - c, a, d) ||
judgePoint24_3(c - b, a, d) ||
judgePoint24_3(b * c, a, d) ||
judgePoint24_3(b / c, a, d) ||
judgePoint24_3(c / b, a, d) ||
judgePoint24_3(a + d, b, c) ||
judgePoint24_3(a - d, b, c) ||
judgePoint24_3(d - a, b, c) ||
judgePoint24_3(a * d, b, c) ||
judgePoint24_3(a / d, b, c) ||
judgePoint24_3(d / a, b, c) ||
judgePoint24_3(a + c, b, d) ||
judgePoint24_3(a - c, b, d) ||
judgePoint24_3(c - a, b, d) ||
judgePoint24_3(a * c, b, d) ||
judgePoint24_3(a / c, b, d) ||
judgePoint24_3(c / a, b, d) ||
judgePoint24_3(a + b, c, d) ||
judgePoint24_3(a - b, c, d) ||
judgePoint24_3(b - a, c, d) ||
judgePoint24_3(a * b, c, d) ||
judgePoint24_3(a / b, c, d) ||
judgePoint24_3(b / a, c, d);
}
};