|
2 | 2 |
|
3 | 3 | import java.util.stream.IntStream; |
4 | 4 |
|
5 | | -/** |
6 | | - * 679. 24 Game |
7 | | - * |
8 | | - * You have 4 cards each containing a number from 1 to 9. |
9 | | - * You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. |
10 | | -
|
11 | | - Example 1: |
12 | | - Input: [4, 1, 8, 7] |
13 | | - Output: True |
14 | | - Explanation: (8-4) * (7-1) = 24 |
15 | | -
|
16 | | - Example 2: |
17 | | - Input: [1, 2, 1, 2] |
18 | | - Output: False |
19 | | -
|
20 | | - Note: |
21 | | - The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12. |
22 | | - Every operation done is between two numbers. |
23 | | - 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. |
24 | | - You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12. |
25 | | -
|
26 | | - */ |
27 | | - |
28 | 5 | public class _679 { |
29 | 6 | public static class Solution1 { |
30 | | - /**Since there are only 4 cards and only 4 operations, we can iterate through all possible combinations, there's a total of 9216 possibilities: |
| 7 | + /** |
| 8 | + * Since there are only 4 cards and only 4 operations, we can iterate through all possible combinations, there's a total of 9216 possibilities: |
31 | 9 | * 1. we pick two out of four cards, with order (since order matters for division), 4 * 3 = 12, then pick one of four operations: 12 * 4 = 48; |
32 | 10 | * 2. then we pick two from these three numbers: 12 * 4 * 3 * 4 * 2 = 1152 |
33 | | - * 3. then we pick the remaining two: 1152 * 2 * 4 = 9216 (with order and out of 4 operations)*/ |
| 11 | + * 3. then we pick the remaining two: 1152 * 2 * 4 = 9216 (with order and out of 4 operations) |
| 12 | + */ |
34 | 13 | public boolean judgePoint24(int[] nums) { |
35 | 14 | return dfs(IntStream.of(nums).mapToDouble(num -> num).toArray()); |
36 | 15 | } |
|
0 commit comments