My Java Solution(Brute Force) Easy Understand


  • 0
    C
    public class Solution {
        public int[] productExceptSelf(int[] nums) {
            int[] res = new  int[nums.length];
            int count = 1;
            int zero = 0;
            int index = 0;
            for(int i=0;i<nums.length;i++){
                if(nums[i] != 0)
                    count *= nums[i];
                if(nums[i] == 0){
                    zero++;
                    index = i;
                }                
            }
            if(zero >= 2)
                return res;
            else if(zero == 1){
                res[index] = count;
                return res;
            }
            else{
                Arrays.fill(res,count);
                for(int i=0;i<nums.length;i++){
                    res[i] = res[i]/nums[i];
                }
                return res;
            }        
        }
    }

  • 0
    C

    Accepted 4ms


Log in to reply
 

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