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. Java 面向对象的帮助文档制作(6)

    set classPath=c:\myclass; 设置path路径

  2. 17.3.12---logging日志模块level配置操作

    1----logging日志记录模块的使用和配置 logging模块我们不需要单独再安装,经常要调试程序,记录程序运行过程中的一些信息,手工记录调试信息很麻烦,所以python的logging模块,会 ...

  3. 吴裕雄--天生自然 PHP开发学习:MySQL 预处理语句

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  4. Anaconda 安装 TensorFlow ImportError:DLL加载失败,错误代码为-1073741795

    错误再现 环境: 使用Anaconda 中 conda 4.6.2, Python 3.7版本 Windows 7 操作系统 CPU: Intel i5 原始安装过程 直接在CMD中,安装链接 中的方 ...

  5. 完成在本机远程连接HBase进行数据增删改查

    1.进行hbase与本机远程连接测试连接 1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字 1.2修 ...

  6. sql plus笔记

    指令请走这边 因为sql plus缓冲区有限 所以要查看输出有时会不太方便 使用spool语句将输出写入文件 sql>spool 要保存的完整路径 ; ; ; sql output; ; ; s ...

  7. 阿里云ECS搭建邮件服务

    安装mailx [root@db ~]# yum install -y mailx [root@db ~]# vim /etc/mail.rc 设置发件人信息 ..... set from=yunwe ...

  8. 画一画BeagleboneBlack的PCB

    一直有听说“Cadence是这个星球上第一好用的EDA软件”,便想着找机会来学学.正好BeagleboneBlack是用Cadence设计的,而且是开源硬件,原理图和PCB文件可以直接在Wiki上下载 ...

  9. spring自定义aop

    package com.dhht.config.articleAdvice; import com.dhht.util.UUIDUtil;import lombok.extern.slf4j.Slf4 ...

  10. webservice SOA

    ------------------认证问题