Seawolf 发表于 2019-9-17 11:50:41

leetcode 238. Product of Array Except Self

Given an array nums of n integers where n > 1,return an array output such that output is equal to the product of all the elements of nums except nums.

Example:

Input:
Output:
Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

class Solution {
    public int[] productExceptSelf(int[] nums) {
      int mul = 1;
      int[] res = new int;
      for(int i = 0; i<nums.length; i++){
            res = 1;
      }
      for(int i = 0; i< nums.length; i++){
            res = res * mul;
            mul = mul * nums;
         }
      mul = 1;
      for(int i = nums.length -1 ; i>=0 ; i--){
            res = res * mul;
            mul = mul * nums;
      }
      
      return res;
    }
}
页: [1]
查看完整版本: leetcode 238. Product of Array Except Self