(medium)LeetCode 238.Product of Array Except Self
Given an array of n integers where n > 1, nums
, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Solve it without division and in O(n).
For example, given [1,2,3,4]
, return [24,12,8,6]
.
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
解法:output[p]=pre[p]*after[p]
代码如下:
public class Solution {
public int[] productExceptSelf(int[] nums) {
int len=nums.length;
int[] output=new int[len];
int[] pre=new int[len];
int[]after=new int[len];
for(int i=0;i<len;i++)
pre[i]=1;
for(int j=0;j<len;j++)
after[j]=1;
for(int m=1;m<len;m++){
pre[m]=pre[m-1]*nums[m-1];
}
for(int n=len-2;n>=0;n--){
after[n]=after[n+1]*nums[n+1];
}
for(int p=0;p<len;p++){
output[p]=pre[p]*after[p];
}
return output;
}
}
运行结果:时间复杂度O(n)
(medium)LeetCode 238.Product of Array Except Self的更多相关文章
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- [leetcode] 238. Product of Array Except Self (medium)
原题 思路: 注意时间复杂度,分别乘积左右两边,可达到O(n) class Solution { public: vector<int> productExceptSelf(vector& ...
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- Java [Leetcode 238]Product of Array Except Self
题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- C#解leetcode 238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
随机推荐
- html之filedset标签
fieldset 元素可将表单内的相关元素分组,当一组表单元素放到 <fieldset> 标签内时,浏览器会以特殊方式来显示它们,它们可能有特殊的边界.3D 效果,或者甚至可创建一个子表单 ...
- 7 款开源 Java 反编译工具
今天我们要来分享一些关于Java的反编译工具,反编译听起来是一个非常高上大的技术词汇,通俗的说,反编译是一个对目标可执行程序进行逆向分析,从而得到原始代码的过程.尤其是像.NET.Java这样的运行在 ...
- MySQL分库分表环境下全局ID生成方案 转
在大型互联网应用中,随着用户数的增加,为了提高应用的性能,我们经常需要对数据库进行分库分表操作.在单表时代,我们可以完全依赖于数据库的自增ID来唯一标识一个用户或数据对象.但是当我们对数据库进行了分库 ...
- DBA日常SQL之查询数据库运行状况
,) Day, ,),,)) H00, ,),,)) H01, ,),,)) H02, ,),,)) H03, ,),,)) H04, ,),,)) H05, ,),,)) H06, ,),,)) H ...
- oracle数据库数据导出和导入
oracle的客户端里面的bin下面有两个可执行文件,名称分别为exp.exe和imp.exe. 他俩的用途就是导出和导入数据用的. 全库 导出:exp 用户名/密码@数据库名 full=y file ...
- 【转】ASP.NET MVC 的最佳实践
[This post is based on a document authored by Ben Grover (a senior developer at Microsoft). It is ou ...
- 【JDBC】向数据表插入数据时,自动获取生成的主键
数据表设计时,一般都会有一个主键(Key)(自己指定),有时也可以使用联合主键: 有许多数据库提供了隐藏列为表中的每行记录分配一个唯一键值(如:rowid): 当我们没有指定哪一列作为主键key时,数 ...
- android学习笔记25——事件处理Handler
Handler消息传递机制 ==> android消息机制是另一种形式的“事件处理”,这种机制主要是为了解决android应用的多线程问题. ——android平台不允许Activity新启动的 ...
- 从网站上复制代码到MyEclipse后每行都是字符编码错误的解决办法
注意图中的工具栏的按钮:Show Whitespace Characters(Window->Preferences->General->Editors->Text Edito ...
- 最大期望算法 Expectation Maximization概念
在统计计算中,最大期望(EM,Expectation–Maximization)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Lat ...