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. 有关uploadifive的使用经验

    这段时间做了一个项目用到uploadifive上传控件,和uploadify不同,这个控件是基于HTML5的版本而不用支持falsh,因而移动端也可以使用. 整理用过的相关属性与方法: 属性 作用 a ...

  2. 【转】linux下安装ssh服务器端及ssh的安全配置

    一.在服务器上安装ssh的服务器端. $ sudo apt-get install openssh-server 2. 启动ssh-server. $ /etc/init.d/sshrestart 3 ...

  3. JavaScript中的防篡改对象

    由于JavaScript共享的特性,任何对象都可以被放在同一环境下运行的代码修改. 例如: var person = {name:"caibin'} person.age = 21; 即使第 ...

  4. ES6 对象增强和结构赋值

    The enhanced Object literals: ES6 has added some new syntax-based extensions to {} object literal fo ...

  5. JS新API标准 地理定位(navigator.geolocation)

    在新的API标准中,可以通过navigator.geolocation来获取设备的当前位置,返回一个位置对象,用户可以从这个对象中得到一些经纬度的相关信息. navigator.geolocation ...

  6. golang os/exec 执行外部命令

    exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...

  7. mybatis原理

    http://blog.csdn.net/column/details/mybatis-principle.html?page=1

  8. `UnityEditor.EditorUtility' does not contain a definition for `GetMiniThumbnail'

    I got the following errors with Untiy 4.0f7error CS0117: `UnityEditor.EditorUtility' does not contai ...

  9. 【adb】adb基本命令总结

    adb常用基本命令如下: adb devices           列出你的devices aapt dump badging <file_path.apk>     查看包名 adb ...

  10. Unity中Time.deltaTime的含义及其应用

    The time in scenes it took to complete the last frame.这是使用此函数的时候给出的提示 一般我们会在设置速度的时候看到这个函数.先写出我对Time. ...