码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode 238]Product of Array Except Self

时间:2019-03-05 18:45:34      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:lag   integer   color   int   bsp   []   nts   复杂度   记录   

记录加入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 }

 

[LeetCode 238]Product of Array Except Self

标签:lag   integer   color   int   bsp   []   nts   复杂度   记录   

原文地址:https://www.cnblogs.com/whl-shtudy/p/10478523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!