题目链接:https://leetcode.com/problems/product-of-array-except-self/

238. Product of Array Except Self

My Submissions

Question
Total Accepted: 36393 Total
Submissions: 87262 Difficulty: Medium

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

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview?

 

Yes

 

No

Discuss

    给出一个数组,要求计算一个新数组。数组里全部的元素都是除了自己以外的元素乘积。而且要求不许用除法。

    《编程之美》上的一道原题。创建两个辅助数组。一个保存全部左边元素乘积的结果。一个保存全部右边元素乘积的结果。借助这两个数组,一次遍历就能够得到结果。

    我的AC代码

public class ProductofArrayExceptSelf {

	public static void main(String[] args) {
int[] a = { 1, 2, 3, 4 };
System.out.print(Arrays.toString((productExceptSelf(a))));
} public static int[] productExceptSelf(int[] nums) {
int len = nums.length;
int[] r = new int[len]; int[] left = new int[len];
int[] right = new int[len];
left[0] = nums[0];
for (int i = 1; i < len; i++) {
left[i] = left[i - 1] * nums[i];
}
right[len - 1] = nums[len - 1];
for (int i = len - 2; i >= 0; i--) {
right[i] = right[i + 1] * nums[i];
} r[0] = right[1];
r[len - 1] = left[len - 2];
for (int i = 1; i < len - 1; i++) {
r[i] = left[i - 1] * right[i + 1];
}
return r;
}
}

LeetCode OJ 238. Product of Array Except Self 解题报告的更多相关文章

  1. 【LeetCode】238. Product of Array Except Self 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 两次遍历 日期 题目地址:https://leetcode.c ...

  2. 【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 ...

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

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

  4. 【刷题-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 ...

  5. LeetCode OJ: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 ...

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

  7. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

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

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

  9. 【九度OJ】题目1052:找x 解题报告

    [九度OJ]题目1052:找x 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1052 题目描述: 输入一个数n ...

随机推荐

  1. Django ORM OneToOneField

    一对一关系 一对一关系与多对一关系非常相似.如果你在模型中定义一个OneToOneField,该模型的实例将可以通过该模型的一个简单属性访问关联的模型. class Person(models.Mod ...

  2. 6-4 破碎的键盘 uva11988

    基础的数组链表 用cur标记光标之前的  用last标记最后的十分巧妙 数组开的不够大会引起RE!! next[0]=0:这使得最后一项所指的为0 核心语句: next[i]=next[cur] ne ...

  3. 在jupyter notebook导入tensorflow出错:No module named tensorflow 解决办法

    1.背景 首先说一下我的环境: os : windows10 anaconda版本:2.7 官网提供了两种方法来安装TensorFlow:pip和anaconda.我使用的是anaconda方法.按照 ...

  4. BootstrapTable使用实例

    一.bootstrapTable简单使用: <link rel="stylesheet" href="./static/libs/bootstrap/css/boo ...

  5. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  6. 003.Kickstart部署之HTTP架构

    一 准备 1.1 完整架构:Kickstart+DHCP+HTTP+TFTP+PXE 1.2 组件应用 Kickstart服务端IP:172.24.8.12 DHCP:提供客户端IP,网关,镜像路径等 ...

  7. js数据结构之二叉树的详细实现方法

    数据结构中,二叉树的使用频率非常高,这得益于二叉树优秀的性能. 二叉树是非线性的数据结构,用以存储带有层级的数据,其用于查找的删除的性能非常高. 二叉树 数据结构的实现方法如下: function N ...

  8. 动态规划-线性dp-hdu-4055

    https://www.cnblogs.com/31415926535x/p/10423047.html 这道题是大连的某一年的现场赛的题hdu-4055 ,,,刚开始做线性dp的题,,看了好半天才看 ...

  9. nodejs那些事儿

    http://www.nodeclass.com/ https://cnodejs.org/ 当前版本,v6.11.2 安装node时,牵扯features的选择,在不了解的情况下,我选择了第1个.网 ...

  10. 专业方向系列-00-Python与有限元初探

    案例1 给出4个弹簧的劲度系数,离散后,求其总的刚度矩阵. 代码: import numpy as np k1, k2, k3, k4 = 500, 250, 2000, 1000 ki = np.a ...