【leetcode81】Product of Array Except Self
题目描述:
给定一个长度为n的整数数组Array【】,输出一个等长的数组result【】,这个输出数组,对应位置i是除了Array【i】之外,其他的所有元素的乘积
例如:
given [1,2,3,4], return [24,12,8,6].
要求:
时间复杂度是o(n)
原文描述:
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].
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
思路1:
- 考虑构造两个数组相乘来解决
例如nums=[a1,a2,a3,a4],构造的数组是:
[1, a1, a1*a2, a1*a2*a3]
[a2*a3*a4, a3*a4, a4, 1]上面的数组相乘,得到[a2*a3*a4, a1*a3*a4, a1*a2*a4, a1*a2*a3]
public class Solution {
public int[] productExceptSelf(int[] nums) {
final int[] result = new int[nums.length];
final int[] left = new int[nums.length];
final int[] right = new int[nums.length];
left[0] = 1;
right[nums.length - 1] = 1;
for (int i = 1; i < nums.length; ++i) {
left[i] = nums[i - 1] * left[i - 1];
}
for (int i = nums.length - 2; i >= 0; --i) {
right[i] = nums[i + 1] * right[i + 1];
}
for (int i = 0; i < nums.length; ++i) {
result[i] = left[i] * right[i];
}
return result;
}
}
思路2:(空间复杂度o(1))
- 考虑上面的第二个数组的数据用一个常数代替,然后输出的数组,是不算空间的
代码:
public class Solution {
public int[] productExceptSelf(int[] nums) {
final int[] left = new int[nums.length];
left[0] = 1;
for (int i = 1; i < nums.length; ++i) {
left[i] = nums[i - 1] * left[i - 1];
}
int right = 1;
for (int i = nums.length - 1; i >= 0; --i) {
left[i] *= right;
right *= nums[i];
}
return left;
}
}
更多leetcode题目,请看我的leetcode专栏。链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode81】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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【数组】Product of Array Except Self
题目: iven an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 【02】[].slice和Array.prototype.slice
[02][].slice和Array.prototype.slice 01,Array是一个构造函数.浏览器内置的特殊对象. 02,Array没有slice方法. 03,Array.prototy ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【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】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
随机推荐
- JAVA 中转义符的理解
生物信息中有时候会遇到JAVA写的程序,今天阅读源码的时候发现对于正则中的转义符不好理解,后来查资料后终于弄明白了,这里详细说明一下: 字符串的表示有三种方法:1.直接单字符,例如"A&qu ...
- [self init]
在字典转模型中遇到了这样的代码: #import "HMAppInfo.h" @implementation HMAppInfo - (instancetype)initWithD ...
- Python列表函数&方法
Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 min(list)返 ...
- Jmeter(二十)_Mock接口
首先解释一下什么是mock接口. Mock通常是指,在测试一个对象时,我们构造一些假的对象来模拟与其交互.而这些Mock对象的行为是我们事先设定且符合预期.通过这些Mock对象来测试对象在正常逻辑,异 ...
- 状态模式、职责链模式——省去if-else的繁琐结构
小时候写日记都是这么写的:上午七点起床,八点之前洗脸刷牙吃早饭,十二点之前好好上课,中午一点,吃午饭,下午两点到六点,上课,下课,找请假,明天妈妈要带我去姥姥家,九点之前,看动画片,九点钟,收拾去姥姥 ...
- 利用git pull的勾子实现敏捷部署
监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- 18 UI美化之level(等级显示显示)
根据level显示哪张图片 在工程文件的res/drawable/新建level-list 如下 <?xml version="1.0" encoding="utf ...
- Microsoft Dynamics CRM 2013/2015 选项集的多选
CRM中的选项集多选一直是客户需求中的必选项,但从CRM进国内的3.0时代开始到目前的2015版本均没有提供该功能,但既然客户要了就得想办法满足,既然CRM本身的功能上不支持,那我们只有使用非官方支持 ...
- 安卓开发过程中空指针的问题Java.lang.NullPointerException
最近做一个新闻客户端的应用,经常出现空指针的问题,我想一方面可能是自己水平有限,二是开发过程中有一些遗漏的地方.一般情况下新手出现空指针的概率较高.下面来总结一下经常出现的问题. 1.所谓的指针,就是 ...