为有牺牲多壮志,敢教日月换新天。

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

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2019-02-21 20:02  为敢技术  阅读(325)  评论(0编辑  收藏  举报