[553. Optimal Division] C++_O(n)


  • 2

    ....Pretty tricky question..

    class Solution {
    public:
     string optimalDivision(vector<int>& nums) {
        if(nums.empty()) return "";
        string res = to_string(nums[0]);
        if(nums.size() > 2){
            res += "/(";
            for(int i = 1; i < nums.size(); ++i){
                res += to_string(nums[i]) + '/';
            }
            res.pop_back();
            res += ')';
        }else if(nums.size() == 2){
            res += '/' + to_string(nums[1]);
        }
        return res;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.