Product of Array Except Self

Total Accepted: 26470 Total Submissions: 66930 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 numsexcept 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.)

限定了不能用除法,和时间复杂度。

但Follow up我没太懂,是不允许申请额外的空间吗?

这次本来想用位运算中的异或运算写,但是发现那样有固有缺陷——对负数不起效果。而且越改越臃肿。

最后在Discuss中找到一个好方法, 这个方法申请了额外的数组。

优点:

  1. 得到数组中新的一个数字时,借助了上一位,从而不用再次遍历整个数组
  2. 方法很巧妙

下面是看完解答后自己写的:

Java语言写的

 public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[0] = 1;
for (int i = 1; i < nums.length; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}
 

【08_238】Product of Array Except Self的更多相关文章

  1. 【leetcode81】Product of Array Except Self

    题目描述: 给定一个长度为n的整数数组Array[],输出一个等长的数组result[],这个输出数组,对应位置i是除了Array[i]之外,其他的所有元素的乘积 例如: given [1,2,3,4 ...

  2. 【LeetCode】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. 【数组】Product of Array Except Self

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

  4. 【02】[].slice和Array.prototype.slice

    [02][].slice和Array.prototype.slice 01,Array是一个构造函数.浏览器内置的特殊对象.   02,Array没有slice方法. 03,Array.prototy ...

  5. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  6. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  7. 【LeetCode 238】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 ...

  8. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  9. 【转载】Java集合类Array、List、Map区别和联系

    Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...

随机推荐

  1. iScroll滚动区域中select、input、textarea元素无法点击的Bug修复

    最近在一个项目中使用了iScroll4模拟滚动效果,调试过程中发现一个表单页中的所有表单项都无法点击聚焦, 如<select>.<input>.<textarea> ...

  2. javascript垃圾回收机制

    js中垃圾回收的算法一般包括两种,一种是“清除标记”,另一种是“引用计数”,现在较为流行的是第一种. “引用计数”现在基本已经被抛弃,主要原因是会导致循环引用,从而导致严重的问题(ie9之前的版本DO ...

  3. 前端开发框架Bootstrap和KnockoutJS

    江湖中那场异常惨烈的厮杀,如今都快被人遗忘了.当年,所有的武林同道为了同一个敌人都拼尽了全力,为数不多的幸存者心灰意冷,隐姓埋名,远赴他乡,他们将唯一的希望寄托给时间.少年子弟江湖老,红颜少女的鬓边也 ...

  4. Android Service 文档

    应用场景: 1  用于将后台逻辑(Service中)和UI逻辑(Activity中)进行解耦,实现Service功能的复用,为其他程序提供功能. 2  后台功能,由于Activity在进入后台时(On ...

  5. Eclipse 和 HSQLDB: 将关系数据库服务器嵌入到 Eclipse 中,第 2 部分

    HSQLDB 开发者角色 对 HSQLDB 与 Eclipse 工作台的集成感兴趣的开发者可以很容易地被分为两类: 客户机开发者,他们只是用 HSQLDB 来存储数据. 引擎开发者,他们通过添加新的标 ...

  6. 怎么去除google的 安全搜索

    想要避开安全搜索 更改右上角的搜索设置,将搜索语言改为英文,然后保存搜索设置 第二次进入搜索设置里找Filter explicit results前的面的勾去掉即可.

  7. Egret Wing3 FTP使用方法

    FTP 挺实用的,不用自己去申请sinasea什么的免费空间来测试项目了. 添加FTP服务器配置 默认就行. 指定目录上传至FTP服务器 选择免费云测试空间.然后选择bin-release/web目录 ...

  8. android自定义进度圆与定时任务

    先看代码:自定进度圆 public class ProgressCircle extends View { private Paint paint; private int strokewidth = ...

  9. Oracle 数据整理

    /* 大数据这块用到了 Oracle ... 记录一下. */ SELECT ssn,password FROM (Select ROWNUM AS ROWNO, T.* from ACCOUNT T ...

  10. System.DateTime.Now的内容

    ?System.DateTime.Now{2016/10/09 15:19:12}    Date: {2016/10/09 0:00:00}    dateData: 985948826838121 ...