679. 24 Game
package LeetCode_679 /** * 679. 24 Game * https://leetcode.com/problems/24-game/ * * 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. * */ class Solution { /* * solution: DFS,recursion, Time complexity:O(1), Space complexity:O(1) * */ fun judgePoint24(nums: IntArray): Boolean { val cards = ArrayList<Double>() for (num in nums) { cards.add(num.toDouble()) } return dfs(cards) } //take two card to calculate each dfs operation private fun dfs(list: List<Double>): Boolean { if (list.size == 1) { //check if can reach 24 if (Math.abs(list.last() - 24.0) < 0.001) { return true } return false } //take out 2 cards each time for (i in list.indices) { for (j in i + 1 until list.size) { //possible combinations val a = list[i] val b = list[j] val combinations = arrayListOf(a + b, a - b, b - a, a * b) //check a,b if 0, because divisor can not be 0 if (a > 0) { combinations.add(b / a) } if (b > 0) { combinations.add(a / b) } for (item in combinations) { val nextRound = ArrayList<Double>() nextRound.add(item) for (k in list.indices) { //if this card had calculated in this round, remove it for next round if (k == i || k == j) { continue } nextRound.add(list[k]) } if (dfs(nextRound)) { return true } } } } return false } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)