leetcode日记 Product of Array Except Self
Given an array of n integers where n > 1,
nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[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的更多相关文章
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
- (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 ...
- 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] ...
- 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 ...
- [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 ...
- 剑指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] * ...
随机推荐
- 【解决方法】magento paypal快速结账 不跳转
magento paypal Express Checkout(快速结账) 页面不跳转到Paypal的解放方法 我使用的magento 1.9.0.1 版本的,Paypal 快速结账都已经设置完毕,但 ...
- SQL查出异常数据(ORA-01722: 无效数字)
-- Created on 2015/4/29 by MENGHU DECLARE -- Local variables here I INTEGER; BEGIN FOR OPEN_DATA IN ...
- Error:No suitable device found: no device found for connection "System eth0"
环境描述: Vmware 故障说明: 在克隆几台虚拟机,发现启动后不能配置IP地址等信息,使用linux命令: “ifup eth0”也不能激活网卡, 而在使用"service networ ...
- Java分别与MySQL、Oracle、SQL Server数据库建立连接
1.与MySQL连接 jar包下载地址: Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动(MySQL的jar包) String u ...
- soapUI请求参数Style与Level使用
http://blog.sina.com.cn/s/blog_71bc9d680102wsuw.html 1.2.资源参数 在这一节中,我们更为详细的看看提供给你不同类型的REST参数.有五种类型的可 ...
- 20个人艰不拆的事实:知道真相的我眼泪掉下来 T.T
20个人艰不拆的事实:知道真相的我眼泪掉下来 T.T 原文链接http://www.u148.net/article/113612.html 来源:ruoning WuMo是丹麦画家Mikael Wu ...
- cin, cin.getline等函数
char s[100]; cin>>s; // 输入一个字符串,遇“空格”.“TAB”.“回车”都结束 cin.getline(s, 20); // cin.get( ...
- Java中值传递和引用传递的概念
很多书中都提到了在Java中只存在值传递,但是今天在一个NanoHTTPD的源码中看到这样一段: if (qmi >= 0) { decodeParms(uri.substring(qmi + ...
- SAP连接电脑串口读数(电子称,磅等数据读取)
这是几年前做的了,一直都不想分享出来,后来想想为了能够给大家点想法,献出来了... 这是一个电脑读称的方法,一般用COMM口连接的电子设备都可参考. 如果是对串口参数不确定的,可以网上找个串口测试工具 ...
- ZACC_DOCUMENT
method if_ex_acc_document~change. data: wa_extension type bapiparex, ext_value() type c, wa_accit ty ...