Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 595 Bytes

File metadata and controls

37 lines (27 loc) · 595 Bytes

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:
Input: [4, 1, 8, 7]
  1. Output:

True

  1. Explanation:
The arithmetic expression is equivalent to an expression tree where parenthesis eliminated. Here are possible solutions using brute force search of possible tree structures, we find:

8*(7-4/1)
8*(4/1-7)
(4-8)*(1-7)
(4-8)*(7-1)
(8-4)*(1-7)
(8-4)*(7-1)
4*8-1+7
1+7-4*8
(1-7)*(4-8)
(1-7)*(8-4)
(7-1)*(4-8)
(7-1)*(8-4)