LeetCode 679. 24 点游戏

原题目: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);
	}
};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值