553. Optimal Division

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求
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 maximum result, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.
这里写图片描述

对于一个连除的数字序列,通过加括号的方式使得最后所得的结果最大。

2,题目思路
对于这道题,如果对于一个连除的序列:a/b/c/d/e
因此,如果想要使得相除结果最大,自然是a/(b/c/d/e)这种形式。

3,程序源码

class Solution {
public:
    string optimalDivision(vector<int>& nums) {
        string res = to_string(nums[0]);
        if(nums.size() == 1)    return res;
        if(nums.size() == 2)    return res + "/" + to_string(nums[1]);
        res = res +  "/" + "(" + to_string(nums[1]);
        for(int i = 2;i<nums.size();i++)
            res += "/" + to_string(nums[i]);
        res += ")";
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值