[Lintcode]739. 24 Game/[Leetcode]679. 24 Game

?[Lintcode]739. 24 Game/[Leetcode]679. 24 Game

本题难度: Hard/Medium

Description

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:

The division operator?/?represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
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.
You cannot concatenate numbers together. For example, if the input is?[1, 2, 1, 2], we cannot write this as 12 + 12.

我的代码


import sys
class Solution:
? ??
? ? def judgePoint24(self, nums: List[int]) -> bool:
? ? ? ? def cal(a,b):
? ? ? ? ? ? if a == 0:
? ? ? ? ? ? ? ? return b,-b,0
? ? ? ? ? ? if b == 0:
? ? ? ? ? ? ? ? return a,-a,0
? ? ? ? ? ? return list({a+b,a-b,b-a,a*b,a/b,b/a})
? ? ? ? stack = [[float(item) for item in nums]]
? ? ? ? while (len(stack) > 0):
? ? ? ? ? ? tmp_nums = stack.pop()
? ? ? ? ? ? if len(tmp_nums) == 1:
? ? ? ? ? ? ? ? if abs(tmp_nums[0] - 24.0) <= 0.0001:
? ? ? ? ? ? ? ? ? ? return True
? ? ? ? ? ? ? ? continue
? ? ? ? ? ? l = len(tmp_nums)
? ? ? ? ? ? for n1 in range(l - 1):
? ? ? ? ? ? ? ? for n2 in range(n1 + 1,l):
? ? ? ? ? ? ? ? ? ? tmp_cals = cal(tmp_nums[n1],tmp_nums[n2])
? ? ? ? ? ? ? ? ? ? for tmp_cal in tmp_cals:
? ? ? ? ? ? ? ? ? ? ? ? tmp_res = [tmp_cal] + tmp_nums[:n1] + tmp_nums[n1 + 1:n2] + tmp_nums[n2 + 1:]
? ? ? ? ? ? ? ? ? ? ? ? stack.append(tmp_res)
? ? ? ? return False ? ? ? ? ? ? ??
? ? ? ? ? ? ? ??

原文地址:https://www.cnblogs.com/siriusli/p/11191944.html

时间: 2024-10-08 16:31:48

[Lintcode]739. 24 Game/[Leetcode]679. 24 Game的相关文章

[LeetCode] 679. 24 Game(回溯法)

传送门 Description 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

Leetcode 679.24点游戏

24点游戏 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) * (7-1) = 24 示例 2: 输入: [1, 2, 1, 2] 输出: False 注意: 除法运算符 / 表示实数除法,而不是整数除法.例如 4 / (1 - 2/3) = 12 . 每个运算符对两个数进行运算.特别是我们不能用 - 作为一元运算符.例如,[1, 1, 1, 1] 作为输

LeetCode题679 —— 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,

Leetcode之深度优先搜索&amp;回溯专题-679. 24 点游戏(24 Game)

深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) * (7-1) = 24 示例 2: 输入: [1, 2, 1, 2] 输出: False 注意: 除法运算符 / 表示实数除法,而不是整数除法.例如 4 / (1 - 2/3) = 12 . 每个运算符对两个数进行运算.特别是我们不能用 - 作为一元运算符.例如,[1, 1

[Leetcode][Python]24: Swap Nodes in Pairs

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 24: Swap Nodes in Pairshttps://oj.leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you shoul

【leetcode】24. Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. 解题分析: 解题思路很简单,就是先两两扫描,然后调换顺序即可.这道题的难点在于每个节点的next域指向问题,所以解这样的题最好可以在纸上写一下交换的步骤就不难实现 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val

LeetCode 笔记24 Palindrome Partitioning II (智商碾压)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

笔试题37. LeetCode OJ (24)

这个题的意思是将一个链表,按照每两个节点逆序.我想了一下,主要是对链表的操作以及对指针的交换,掌握了方法以后确实不是很难,但是需要注意的是不要将指针给弄错了,否则容易导致链表信息丢失. 我的方法中使用到了四个主要指针,我画了个图来解释它们: 这四个指针的作用通过上图,大家应该能够理解了,因为我们需要做的事有三件: (1)交换中间部分 (通过prev和cur) (2)连接前面部分(通过pprev) (3)连接后面部分(通过next) 代码如下: /** * Definition for singl

LeetCode(24) Swap Nodes in Pairs

题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,