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. LNMP安装问题

    查什么占用了端口   netstat -nlp |grep :80 root@zzx:/usr/local/mysql# netstat -nlp |grep :80tcp        0      ...

  2. AES学习小结

    AES是基于数据块的加密方式,即每次处理的数据是一块(16字节),当数据不是16字节的倍数时填充,这就是所谓的分组密码(区别于基于比特位的流密码),16字节是分组长度. AES支持五种模式:CBC,C ...

  3. spring获取当前动态代理对象

    由于spring中的aop拦截的是代理对象 当拦截的目标方法被本类的另一个方法调用时,会出现拦截失效 最佳实践: 例如拦截RegulationService的update(regulation)方法, ...

  4. MyBatis从入门到精通(第9章):Spring集成MyBatis(上)

    MyBatis从入门到精通(第9章):Spring集成MyBatis(上) Spring是一个为了解决企业级Web应用开发过程中面临的复杂性,而被创建的一个非常流行的轻量级框架. mybatis-sp ...

  5. windows服务器搭建SVN[多项目设置方法]

    https://tortoisesvn.net/downloads.html 根据系统版本进行下载,下载后正常一路正常安装. 第一.设置版本号仓库目录,比如:cdengine 第二.在cdengine ...

  6. ZJNU 2201 - 挖矿谷物语

    在dfs过程中加上栈记录当次dfs走过的路径 如果当次dfs到了一个之前的dfs已经经过的点 又因为只对没有访问过的点开始dfs 所以这种情况就说明接下来不可能返回到当次dfs开始的点 将栈内元素取出 ...

  7. springboot配置多个yml文件

    新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...

  8. macbook 一些php相关操作

    开启php: https://jingyan.baidu.com/article/67508eb434539f9cca1ce4da.html 配置多虚拟主机:  https://jingyan.bai ...

  9. Qt QString 和 LPCWSTR 的相互转换

    在windosw 编程中,常用到LPCWSTR 变量,QT中最常用到QString,下面提供QString和LPCWSTR 相互转换的方法 LPWSTR 转换成QString LPCWSTR str; ...

  10. k8s miniKube 入门

    k8s miniKube 入门 miniKube 是单机版kubernetes, 可以配置运行在同一台主机上的服务和pod,并使用docker作为虚拟化工具 下载:直接下载可执行文件,复制到path ...