zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    k8s--容器挂载 error: /proc must be mounted
    mysql--read only
    C#读取Excel文件(.xls .xlsx)
    如何使用BBCode
    markdown使用经验积累
    openlayers学习之-----入门篇
    echarts学习之----动态排序柱状图
    echarts学习之----多图例折线图
    Web3D学习之-----全景图预览插件photo-sphere-viewer
    vue报错解决----npm ERR!
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13954173.html
Copyright © 2011-2022 走看看