[LeetCode 238]Product of Array Except Self

记录加入Datawhale第7天,养成每天做题的好习惯

题目描述:

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

本题目前做法很垃圾,没有达到要求,只能勉强通过,时间复杂度没有达到要求(后面考虑新方法立flag)

Java代码:

 1 class Solution {
 2     public int[] productExceptSelf(int[] nums) {
 3         int[] output = new int[nums.length];
 4         for(int i = 0;i < output.length;i++){
 5             output[i] = 1;
 6         }
 7         for(int i = 0;i < nums.length;i++){
 8             for(int j = 0;j < output.length;j++){
 9                 if(i != j){
10                     output[j] = output[j] * nums[i];
11                 }
12             }
13         }
14         return output;
15     }
16 }

猜你喜欢

转载自www.cnblogs.com/whl-shtudy/p/10478523.html