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;
}
};