simple java solution


  • -1
    K
    public int[] productExceptSelf(int[] nums) {
            int product=1;
            int zeros=0;
            int[] res=new int[nums.length];
            for(int num:nums){
                if (num==0){zeros++;product*=1;}
                else if(num!=0)product*=num;
                if(zeros>1)return res;
            }
            int index=0;
            for (int num: nums){
                if (num==0)res[index++]=product;
                else if(zeros==1&&num!=0)res[index++]=0;
                else res[index++]=product/num;
            }
            return res;
        }
    

Log in to reply
 

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