• [Swift]LeetCode553. 最优除法 | Optimal Division


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10414956.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.

    However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the maximumresult, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.

    Example:

    Input: [1000,100,10,2]
    Output: "1000/(100/10/2)"
    Explanation:
    1000/(100/10/2) = 1000/((100/10)/2) = 200
    However, the bold parenthesis in "1000/((100/10)/2)" are redundant, 
    since they don't influence the operation priority. So you should return "1000/(100/10/2)". Other cases: 1000/(100/10)/2 = 50 1000/(100/(10/2)) = 50 1000/100/10/2 = 0.5 1000/100/(10/2) = 2 

    Note:

    1. The length of the input array is [1, 10].
    2. Elements in the given array will be in range [2, 1000].
    3. There is only one optimal division for each test case.

    给定一组正整数,相邻的整数之间将会进行浮点除法操作。例如, [2,3,4] -> 2 / 3 / 4 。

    但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级。你需要找出怎么添加括号,才能得到最大的结果,并且返回相应的字符串格式的表达式。你的表达式不应该含有冗余的括号。

    示例:

    输入: [1000,100,10,2]
    输出: "1000/(100/10/2)"
    解释:
    1000/(100/10/2) = 1000/((100/10)/2) = 200
    但是,以下加粗的括号 "1000/((100/10)/2)" 是冗余的,
    因为他们并不影响操作的优先级,所以你需要返回 "1000/(100/10/2)"。
    
    其他用例:
    1000/(100/10)/2 = 50
    1000/(100/(10/2)) = 50
    1000/100/10/2 = 0.5
    1000/100/(10/2) = 2
    

    说明:

    1. 输入数组的长度在 [1, 10] 之间。
    2. 数组中每个元素的大小都在 [2, 1000] 之间。
    3. 每个测试用例只有一个最优除法解。

    Runtime: 8 ms
    Memory Usage: 18.6 MB
    1 class Solution {
    2     func optimalDivision(_ nums: [Int]) -> String {
    3         if nums.count <= 2 {
    4         return nums.map({"($0)"}).joined(separator: "/")
    5     }
    6     return "(nums.first!)" + "/(" + nums[1...].map({"($0)"}).joined(separator: "/") + ")"
    7     }
    8 }

    12ms

     1 class Solution {
     2     func optimalDivision(_ nums: [Int]) -> String {
     3         var ans = nums.map { String($0) }
     4 
     5         if nums.count < 3 {
     6             return ans.joined(separator: "/")
     7         }
     8 
     9         return ans[0] + "/(" + ans[1..<ans.count].joined(separator: "/") + ")"
    10     }
    11 }

    20ms

     1 class Solution {
     2     func optimalDivision(_ nums: [Int]) -> String {
     3         var res:String = String()
     4         var n:Int = nums.count
     5         for i in 0..<n
     6         {
     7             if i > 0 {res += "/"}
     8             if i == 1 && n > 2 {res += "("}
     9             res += String(nums[i])
    10             if i == n - 1 && n > 2 {res += ")"}
    11         }
    12         return res
    13     }
    14 }
  • 相关阅读:
    单目标遗传算法 精英保留策略
    单目标优化问题 常用的 测试函数(MATLAB版)
    单目标优化问题 常用的 测试函数
    叼丝装备之服装必备----111111111111111111111111111111111
    算法优化之车牌识别---车牌识别优化项
    医疗器械与图像处理行业简介
    图像处理之增强---高斯模糊
    图像增强之拉普拉斯锐化---高斯一阶导二阶导数
    图像特效之油画---类似油画效果
    图像出增强之锐化---拉普拉斯锐化
  • 原文地址:https://www.cnblogs.com/strengthen/p/10414956.html
Copyright © 2020-2023  润新知