Is this question a joke? [7ms easy java solution]


  • 2
    R

    The answer has such pattern:
    if nums.length == 1 ------ first-element itself.
    if nums.length == 2 ------ first-element / second-element.
    if nums.length > 2 ------ first-element / ( the other elements, separated by '/' ) , no matter what.

    public class Solution {
        public String optimalDivision(int[] nums) {
            String str = "";
            if(nums.length == 0){
                return str;
            }
            str = str + nums[0];
            if(nums.length == 1){
                return str;
            }
            str = str + "/";
            if(nums.length == 2){
                return str + nums[1];
            }
            str = str + "(" + nums[1];
            for(int i = 2; i < nums.length; i++){
                str = str + "/" + nums[i];
            }
            str = str + ")";
            return str;
        }
    }
    

Log in to reply
 

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