You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through */+-() to get the value of 24.

Example 1:

Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24

Example 2:

Input: [1, 2, 1, 2]
Output: False

Note:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

思路:把每两个数拿出来,找出其所有的加减乘除组合,然后把每一个取出来,后剩余的两个数(总共也就是3个数),继续做同样的操作,直到只剩下一个数,看那个数是不是24.

 class Solution {
public boolean judgePoint24(int[] nums) {
return helper(new double[] { nums[], nums[], nums[], nums[] });
} private boolean helper(double[] nums) {
if (nums.length == ) return Math.abs(nums[] - ) < 0.01;
for (int i = ; i < nums.length; i++) {
for (int j = i + ; j < nums.length; j++) {
double[] remainings = new double[nums.length - ];
int idx = ;
// add numbers in nums to remainings excluding nums[i] and nums[j]
for (int k = ; k < nums.length; k++) {
if (k != i && k != j) {
remainings[idx] = nums[k];
idx++;
}
}
for (double k : computeCombinations(nums[i], nums[j])) {
remainings[remainings.length - ] = k;
if (helper(remainings)) {
return true;
}
}
}
}
return false;
} private double[] computeCombinations(double a, double b) {
return new double[] { a + b, a - b, b - a, a * b, a / b, b / a };
}
}

24 Game的更多相关文章

  1. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  2. CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能

    CSharpGL(24)用ComputeShader实现一个简单的图像边缘检测功能 效果图 这是红宝书里的例子,在这个例子中,下述功能全部登场,因此这个例子可作为使用Compute Shader的典型 ...

  3. 【趣味分享】C#实现回味童年的24点算法游戏

    一.24点游戏玩法规则效果展示 1.初始化界面 2.开始游戏界面 3.游戏超时界面 4.查看答案界面 5.答对界面 6.答错界面 7.计算表达式的验证界面 8.一副牌算完开始新一副牌界面 到这里24点 ...

  4. C#开发微信门户及应用(24)-微信小店货架信息管理

    在前面微信小店系列篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及<C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试& ...

  5. [MySQL Reference Manual] 24 MySQL sys框架

    24 MySQL sys框架 24 MySQL sys框架 24.1 sys框架的前提条件 24.2 使用sys框架 24.3 sys框架进度报告 24.4 sys框架的对象 24.4.1所有sys下 ...

  6. mysql 5.6.24安装实例

    安装前准备工作: 1)编辑PATH路径 vim /etc/profile PATH=/home/mysql/bin:/home/mysql/lib:$PATH export PATH 2)生效PATH ...

  7. C语言-纸牌计算24点小游戏

    C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...

  8. .NET开发人员必看:提高ASP.NET Web应用性能的24种方法和技巧

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  9. [.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下)

    [.net 面向对象程序设计进阶] (24) 团队开发利器(三)使用SVN多分支并行开发(下) 本篇导读: 接上篇继续介绍SVN的高级功能,即使用分支并行开发.随着需求的不断变更,新功能的增加.特别是 ...

  10. 1Z0-053 争议题目解析24

    1Z0-053 争议题目解析24 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 24.Which of the following information will be gath ...

随机推荐

  1. 4月25日课上练习 一维数组最大子数组(debug版)

    一维数组中求最大子数组的算法 package com.wangwang.mar; import java.util.Scanner; public class Sum { public static ...

  2. [转帖]Windows Server 2016各种版本介绍

    Windows Server 2016各种版本介绍 http://www.5sharing.com/js/zx/872.html windows server的版本 时间:2018-10-06 10: ...

  3. SpringBoot配置activemq消息队列

    1.配置pom相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  4. Vue接口异常时处理

    一般接口只会对后台返回的json状态进行判断处理,当后台异常时,我们可以使用catch来对这些异常进行同样的报错处理. 例如: 上面显示代码例子中test为一个接口,json为后台正常返回的数据对象, ...

  5. 在Ubuntu 18.04系统上安装Systemback的方法(抄)

    在Ubuntu 18.04系统上安装Systemback的方法 2018-12-26 21:39:05作者:林莉稿源:云网牛站 本文介绍如何在Ubuntu 18.04或者Ubuntu 18.10系统上 ...

  6. Helicute FPV App Privacy Policy

    Personal Data collected for the following purposes and using the following services: Device permissi ...

  7. 「ZJOI Day2」游记

    Day-1 晚上一直在出自己做的模拟赛的T1,真的快要死掉了. 分类讨论几十种情况. 窝还是找了Bluesky大佬一起来验题,她瞬间就A掉了这一道题目...自闭了.. 诶,我还是太弱了. 之前教练组织 ...

  8. placeholder中文字添加换行方法

    需求: 文本域内的提示文字两行显示 解决方案: 表示回车 表示换行 <textarea id="textarea" maxlength="22" plac ...

  9. CF1129B 【Wrong Answer】

    既然 $ n \leq 2000$ 那我们就假使所有的 $n = 2000 $ 主要是为了方便.再使 \(x = \sum_{i=1} ^ {1999}\) 以及 $a_1=a_2=a_3=...=a ...

  10. win 10 dpi 缩放

    win 10 dpi dwm 效果,影响最大的函数有 参考资料:https://blog.csdn.net/chenlycly/article/details/53142098 GetSystemMe ...