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] * ...
随机推荐
- jquery on 绑定多个事件 多个元素
$('.wrap').on({ click:function(){ //事件1 ...... }, keyup:function() { //事件2 ....... }, keydown:functi ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据
Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...
- 【网络】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)
1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...
- 浅谈android binder机制
binder机制 是谷歌优化在android上更适合终端的IPC(多进程通信方式),满足系统对通信方式,传输性能和安全性的要求. 特性: 1. 用驱动程序来推进进程间的通信.2. 通过共享内存来提高性 ...
- 强大的打印功能jatoolsPrinter使用总结
最近功能做项目,需要实现打印条码标签的功能,对于第一次接触打印机的小白来说简直是折磨死我拉,公司采购的打印机是斑马的GK888T,其实,如果单纯的想实现能打印出来标签的话,直接用window.prin ...
- SSH框架总结
首先,SSH是由多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框架的系统从职责 ...
- Swift函数的定义
//: Playground - noun: a place where people can play import Cocoa //基本的函数 //************************ ...
- express+gulp构建项目(二)启动项目和主文件
这一次整理的内容是项目主文件和如何启动项目. 启动项目 通过nodejs官网的例子https://nodejs.org/docs/latest-v4.x/doc/api/synopsis.html我们 ...
- 【转】ArcGIS地图缓存制作简介
[PDF]ArcGIS 地图缓存制作简介 - Esri中国 ArcGIS地图缓存制作简介 制作好的电子地图只有发布为服务后才能为更多的用户所查看与使用.ArcGIS Server 为共享 GIS 资源 ...
- 安装linux操作系统
安装双操作系统; 1 0. 介绍: 1 1 实验环境: 2 2. 实验准备: 2 3.开始安装: 2 1 制作U盘启动工具: 2 2.安装LinuxOS. 3 2.1在windowOS中划分60G空间 ...