6-10PM challenge problem #002 solution

Problem#002

Array of Array Products

public  int[] solve(int[] arr) {
       int [] out = new int[arr.length];
       int product =1;

       for(int i=0;i<arr.length;i++){
           out[i]=product;
           product = product*arr[i];
        }

        product=1;
        for(int j=arr.length-1;j>=0;j--){
            out[j] = out[j]*product;
            product = product* arr[j];
        }
      return out;
   }

n is size of an array
Time complexity is O(n)
Space complexity is O(n)

原文链接:6-10PM challenge problem #002 solution

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容