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] * ...
随机推荐
- linux网卡混杂模式
混杂模式就是接收所有经过网卡的数据包,包括不是发给本机的包,即不验证MAC地址.普通模式下网卡只接收发给本机的包(包括广播包)传递给上层程序,其它的包一律丢弃.一般来说,混杂模式不会影响网卡的正常工作 ...
- sql server项目死活启动不了的问题
报错没截图, 关键字"sessionfactory",tomacat 能够启动,但是网页打开是错误的 解决:c盘索引文件冲突,删除了 发生数据库c3p0错误,tomcat启动没玩没 ...
- 《高级Web应用程序设计》课程
一.课堂课件 全部授课内容 二.作业 访问ftp://192.168.42.254:22,登录后找到自己的姓名文件夹,放入作业即可.登录账号为stu1,密码为空. 已布置练习 练习1(截止日期10月1 ...
- mysql 5.7 忘记root密码解决方法
1.修改配置文件 vim /etc/my.cnf 在[mysqld]节点添加 skip-grant-tables 2.重启mysql 3.用空密码进入 mysql -uroot 执行 update m ...
- 实验三——for 语句及分支结构else-if
1.本节课学习到的知识点:在本次课中,我学习了for语句的使用,认识了for语句的执行流,明确了三种表达式的意义.以及最常用的实现多分支的else-if语句. 2.实验过程中遇到的问题及解决方法:在本 ...
- hihocoder-平衡树·SBT
http://hihocoder.com/problemset/problem/1337 #1337 : 平衡树·SBT 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 ...
- Linux 系统把英文修改成中文界面
1.一般安装后的linux系统都是英文的界面,网上查了一下各种说法都有,我只做了如下的配置就好了,下载个中文包,改一下i18n就完事了,并没有那么复杂 下面上图文: 目前是英文的界面 2.下载个中文包 ...
- 突破php的imagecopyresampled 和imagecopyresized 实现图片马JPG
编辑器加载中...之前有人发布了 利用PNG 图片上述压缩函数的方法 原理利用 PNG的结构IDAT chunks填充一句话webshell,并进行一套取模运算 详见: https://www.ido ...
- 直接在 CSS 中引用 FONTAWESOME 图标(附码表)
直接在 CSS 中引用 FONTAWESOME 图标(附码表) 因此我们如果不想用 Fontawesome 提供的类,只想在 css 里面引用图标的话,可以这样子,请自行脑补: .icon:befor ...
- gulp详细入门教程-gulp demo download
简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码 ...