【leetcode】553. Optimal Division

题目如下:

解题思路:这是数学上的一个定理。对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN)。

代码如下:

class Solution(object):
    def optimalDivision(self, nums):
        """
        :type nums: List[int]
        :rtype: str
        """
        if len(nums) == 1:
            return str(nums[0])
        elif len(nums) == 2:
            return str(nums[0]) + '/' + str(nums[1])
        res = ''
        for i,v in enumerate(nums):
            if i == 0:
                res = str(v)  + '/' + '('
            else:
                res += str(v)
                res += '/'
        return res[:-1] + ')'

猜你喜欢

转载自www.cnblogs.com/seyjs/p/9630679.html
今日推荐