Java Solution With Two Boolean Variable


  • -2
    S
    public class Solution {
        public int[] productExceptSelf(int[] nums) {
            int product = 1;
            boolean containZero = false, containOverOneZero=false;;
            for(int n:nums){
                if(n != 0)
                    product = product * n;
                else{
                    if(containZero)
                        containOverOneZero = true;
                    containZero = true;
                }
            }
            
            int[] res = new int[nums.length];
            if(containOverOneZero)
                return res;
            for(int i=0;i<nums.length;i++){
                if(containZero){
                    if(nums[i] == 0)
                        res[i] = product;
                }else{
                    res[i] = product / nums[i];
                }
            }
            return res;
        }
    }

  • 4
    D

    "solve it without division",bro.


Log in to reply
 

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