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. 理解浮动和position定位(转)

    前言 为了更好理解浮动和position,建议先看看我写的这篇文章<Html文档流和文档对象模型DOM理解> 正文 一.浮动 CSS设计float属性的主要目的,是为了实现文本绕排图片的效 ...

  2. css3 flex布局详解

    原文链接: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool https://www.cnblog ...

  3. strpos用法

    语法 strpos(string,find,start) 参数 描述 string 必需.规定要搜索的字符串. find 必需.规定要查找的字符串. start 可选.规定在何处开始搜索.   技术细 ...

  4. android studio compile api implementation 区别

    compile与api 二者等同,无区别 implementation与compile或implementation与api implementation编译的依赖只作用于当前的module.即APP ...

  5. React 渲染嵌套对象,内部对象会是undefined

    在编译器中获取数据 发现报错 原因: render()一加载就会渲染,渲染的数据是初始state里的值 ,当setState会再次渲染 解决方法 1.三元运算 判断对象是否存在 2.在初始化对象的时候 ...

  6. quartz定时定时任务执行两次

    quartz框架没问题. 流程: sping-quartz配置 <?xml version="1.0" encoding="UTF-8"?> < ...

  7. [HNOI2019]白兔之舞(矩阵快速幂+单位根反演)

    非常抱歉,这篇文章鸽了.真的没时间写了. #include<bits/stdc++.h> using namespace std; typedef long long ll; #defin ...

  8. \_\_setitem\_\_和\_\_getitem和\_\_delitem__

    目录 __setitem__和__getitem和__delitem__ 一.__setitem__ 二.__getitem__ 三.__delitem__与__delattr__ 四.总结 __se ...

  9. Djang_框架

  10. day57-mysql-五种约束和sql语句逻辑执行顺序

    二.sql语句逻辑执行顺序 () SELECT () DISTINCT <select_list> 去重复 () FROM <left_table> () <join_t ...