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

题目说明:给定一个数组,求一个新数组满足:每一位上的数等于给定数组除开此位之积。

  最先反应过来的想法为求出整个数组的积然后除以每一位即可,不过再一细想,如果数组中有0的存在,那么整个数组的积即为0,那么这样就需要找到0这个位置,然后再算剩下的积,不过又想0元素可以不止一个,不过如果0不止一个的话那么整个数组的结果就均为0,这样便可以解出结果。

  不过题目要求不使用除法,那么应该怎么办呢?

  题目说明需要线性时间来解决,那么就说明很可能只需要遍历一两次即可,如果正向遍历的话,只能得到到达当前位置之前的数的积,并不能知道当前位置之后的数的积。这样一想,只要把正向遍历反过来,就可以得到当前位置之后的数的积,这样再将得到的前后数乘起来,即可得到题目要求答案。

python代码:

    def productExceptSelf(self, nums):
tem1=[1]
tem2=[1]
length=len(nums)
for i in xrange(1,length):
tem=tem1[i-1]*nums[i-1]
tem1.append(tem)
tem=nums[length-i]*tem2[i-1]
tem2.append(tem)
for i in xrange(length):
tem1[i]=tem1[i]*tem2[-(i+1)]
return tem1

这个解法使用了额外的空间,而题目中提了个问题说“Could you solve it with constant space complexity?”,那么这个方法可以优化吗?

很明显,反向所得结果并不需要存下来,只需要存下正向结果,然后实时求出对应位置上的反向结果,直接将结果得出即可:

java代码:

    public int[] productExceptSelf(int[] nums) {
int []result=new int[nums.length];
result[0]=1;
for (int i=1;i<nums.length;i++){
result[i]=result[i-1]*nums[i-1];
}
for (int i=nums.length-2,tem=1;i>=0;i--){//这里不需要从最后一位开始,因为最后一位的反向实时结果为1,与正向结果相乘后结果不变,因此不需要改变
tem*=nums[i+1];
result[i]*=tem;
}
return result;
}

leetcode日记 Product of Array Except Self的更多相关文章

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

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

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

  4. [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 ...

  5. (medium)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 ...

  6. Java [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]  ...

  7. C#解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]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 ...

  9. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

随机推荐

  1. HTML <a> download 属性,点击链接来下载图片

    Html5里面的 标签的 Download 属性可以设置一个值来规定下载文件的名称.所允许的值没有限制,浏览器将自动检测正确的文件扩展名并添加到文件 (.img, .pdf, .txt, .html, ...

  2. oracle 隐藏过长字段

    case                    when length(m.topic)>20 then substr(m.topic,0,20)||'...'                  ...

  3. eclipse中的web环境配置

    一.tomcat的安装 直接官网下载,选择自动配置安装Exe文件. 测试: 1.启动安装目录下bin文件中的exe文件,观察是否安装完好.若出现打断文字黑框,则说明已经安装完好. 2.输入网址.htt ...

  4. 解读浮动闭合最佳方案:clearfix

    .clear{clear:both;height:0;overflow:hidden;} 上诉办法是在需要清除浮动的地方加个div.clear或者br.clear,我们知道这样能解决基本清浮动问题. ...

  5. JS基本概念

    1.一切(变量.函数名.操作符)都区分大小写 2.标识符:第一个字符必须为字母.下划线或者美元符号,其他字符可以是字母.下划线.美元符号或者数字 3.数据类型 1)undefined:用var声明的变 ...

  6. Eclipse 安装中文插件

    从官网下载下来的Eclipse都是英文版,下载后解压,运行Eclipse. 地址:http://www.eclipse.org/babel/downloads.php .启动Eclipse,在菜单,“ ...

  7. 很重要的vue的生命周期

  8. WebForm复杂控件

    Calendar   日历: FileUpdate 文件上传: Image 图片,可以直接给URL: Repeater: HeaderTemplate - 在加载开始执行一遍 ItemTemplate ...

  9. jquery1:

    在jquery中:1.window.onload: --->$();所以一般jquery用如下开头: $(function(){ })2.获取元素:$('#div'):获取id为div的元素$( ...

  10. table表格中的内容溢出布局方式

    什么是内容溢出呢?其实就是当文字很多的时候,如果内容区域只有那么长,那么多出的部分以点点点代替. 这次做的案例是在table里面,我们知道当我们在table里输入过多的文字内容的时候会撑乱表格,例如一 ...