【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 ...
随机推荐
- Jenkins执行批处理文件、powershell失败
今天搭建Jenkins持续集成环境,编译环境是.net core.整理了一些发布的命令配置在Jenkins,问题来了,使用powershell插件运行dotnet restore.dotnet bui ...
- ACM Where is the Marble?
Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers ...
- Node.js 工具模块
在 Node.js 模块库中有很多好用的模块.接下来我们为大家介绍几种常用模块的使用: 序号 模块名 & 描述 1 OS 模块 提供基本的系统操作函数. 2 Path 模块提供了处理和转换文件 ...
- win 10 和 CentOS 7 双系统安装
工具及材料 1.一台PC 2.一个U盘,8G以上 3.需要的文件:CentOS-7-x86_64-DVD-1511.iso 4.需要的软件:UltraI ...
- leetcode 之 Single Number II
问题来源:Single Number II 问题描述:给定一个整数数组,除了一个整数出现一次之外,其余的每一个整数均出现三次,请找出这个出现一次的整数. 大家可能很熟悉另一个题目(Single Num ...
- [extjs5学习笔记]第三十八节 sencha CMD 6.0.0.220版本安装
本文地址:http://blog.csdn.net/sushengmiyan/article/details/46740381 简介 sencha cmd 6安装过程不需要UAC控制了对于widnow ...
- AJAX编程实践
---------------------------------------------------------------------------------------------------- ...
- PHP 验证码 浅析
拓展 背景图 imagecreatetruecolor imagecolorallocate imagepng imagedestoryimage 简易数字验证码 imagecolorallocate ...
- Premake可生成vcxproj.filters
Premake可生成vcxproj.filters (金庆的专栏) 添加 vcxproj.filters 文件可以用目录结构组织源文件. 例如premake5添加所有文件: files { ...
- 给pdf文件添加防伪水印logo(附工程源码下载)
pdf添加水印logo这种需求场景确实很少,有些时候一些销售单据生成pdf添加一个水印logo,做一个简单的防伪效果,虽然实际上并没有太大作用,但是产品经理说要,巴拉巴拉--省略一万字. 下面将源码分 ...