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].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

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) {
if (nums == null || nums.length == 0) {
return nums;
}
int[] resArr = new int[nums.length];
resArr[0] = 1;
for (int i = 1; i < nums.length; i++) {
resArr[i] = resArr[i - 1] * nums[i - 1];
} int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
resArr[i] *= right;
right *= nums[i];
}
return resArr;
}
}
public class Solution {
/**
* @param nums: an array of integers
* @return: the product of all the elements of nums except nums[i].
*/
public int[] productExceptSelf(int[] nums) {
// write your code here
int len = nums.length;
int[] prefix = new int[len];
int[] suffix = new int[len];
for (int i = 0; i < len; i++) {
if (i == 0) {
prefix[i] = nums[i];
continue;
}
prefix[i] = prefix[i - 1] * nums[i];
}
for(int i = len - 1; i >= 0; i--) {
if (i == len - 1) {
suffix[i] = nums[i];
continue;
}
suffix[i] = suffix[i + 1] * nums[i];
}
int[] res = new int[len];
for (int i = 0; i < len; i++) {
if (i == 0) {
res[i] = suffix[i + 1];
continue;
}
if (i == len - 1) {
res[i] = prefix[i - 1];
continue;
}
res[i] = prefix[i - 1] * suffix[i + 1];
}
return res;
}
}

[LC] 238. Product of Array Except Self的更多相关文章

  1. 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 ...

  2. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  3. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  4. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  5. 【LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  6. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  7. 【刷题-LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...

  8. 238. Product of Array Except Self 由非己元素形成的数组

    [抄题]: Given an array of n integers where n > 1, nums, return an array output such that output[i]  ...

  9. [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 ...

随机推荐

  1. php速成_day4

    一.微信公众平台概述 1.微信发展史 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册 ...

  2. Linux ES集群服务配置说明

    说明: ES官网不建议在root用户使用Elastic Server,因此ES集群配置均使用普通账户操作,新建账户 elastic. Linux版本为CentOS 7.3,ES版本为5.5.0. 一. ...

  3. keras中的一些小tips(一)

    写这篇博客的原因主要是为了总结下在深度学习中我们常会遇到的一些问题,以及不知道如何解决,我准备把这个部分作为一个系列,为了让大家少走一些坑,对于本博客有什么错误,欢迎大家指出,下面切入正题吧. 1. ...

  4. python编程:从入门到实践----第四章>操作列表

    一.遍历整个列表 1-1.假设有一个魔术师名单,需要将其中每个魔术师的名字都打印出来. # 用for循环来打印魔术师名单中的名字 magicians=['alice','david','carolin ...

  5. Python—插入排序算法

    # 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...

  6. 第五章——Pytorch中常用的工具

    2018年07月07日 17:30:40 __矮油不错哟 阅读数:221   1. 数据处理 数据加载 ImageFolder DataLoader加载数据 sampler:采样模块 1. 数据处理 ...

  7. Ubuntu源码编译安装tensorflow

    ubuntu14 cuda9.0_384.81 驱动版本384.90  cudnn7.2 tensorflow1.8 https://blog.csdn.net/pkokocl/article/det ...

  8. Django专题-Cookie

      Cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响, ...

  9. 基于Flask框架搭建视频网站的学习日志(六)之数据库

    使用Flask-SQLSlchemy管理数据库(1)--初步安装调试 一.介绍: Flask-SQLSlchemy是一个Flask扩展,简化了Flask中对sql的操作,是一个高层的框架,可以避免直接 ...

  10. HDU - 4578 线段树+三重操作

    这道题自己写了很久,还是没写出来,也看了很多题解,感觉多数还是看的迷迷糊糊,最后面看到一篇大佬的才感觉恍然大悟. 先上一篇大佬的题解:https://blog.csdn.net/aqa20372995 ...