Once you understand that first number has to go top, second has to go bottom, it is easy

  • 1
    class Solution {
        string optimalDivision(vector<int>& nums) {
            int n = nums.size();
            if(n==0) return "";
            if(n==1) return to_string(nums[0]);
            if(n==2) return to_string(nums[0])+"/"+to_string(nums[1]);
            string ret = to_string(nums[0])+"/(";
            for(int k = 1;k<nums.size();k++) {
                if(k<nums.size()-1) ret += to_string(nums[k])+"/";
                else ret += to_string(nums[k])+")";
            return ret;

Log in to reply

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