【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 ...
随机推荐
- 用burpsuite暴力破解后台
[实验原理] Burp Suite是Web应用程序测试的最佳工具之一,其多种功能执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登录表单,执行会话令牌等多种的随机性检查. Burp ...
- 13.QT-QMainWindow组件使用
QMainWindow介绍 主窗口是与用户进行长时间交互的顶层窗口,比如记事本 主窗口通常是应用程序启动后显示的第一个窗口 QMainWindow是Qt中主窗口的基类,继承于QWidget,如下图所示 ...
- 剑指架构师系列-持续集成之Maven实现项目的编译、发布和部署
Maven组织项目进行编译.部署 Maven项目基本的结构说明如下: mazhi // 控制所有荐的编译.部署.发布 mazhi-app-parent // 项目的父项目,有一些公共的设置可以被子 ...
- Django网站制作
创建mysite目录 django-admin.py startproject mysite这个命令作用是:这将创建在当前目录创建一个mysite目录 前提是从命令行上cd到你想储存你代码的目录,然后 ...
- Java第2次实验提纲(Java基本语法与类库)
1. 使用Git克隆(clone)项目到你的Eclipse项目中 见以下参考资料中的3 从码云将项目clone到你的电脑 重要提示: 使用Git来管理你的代码以后,当你在本机Eclipse项目中开始编 ...
- 防止SpringMVC拦截器拦截js等静态资源文件
SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...
- 新版Azure CDN HTTPS加速服务正式上线
随着网络安全问题日益得到全民重视,HTTPS网络访问协议在互联网访问中得到了广泛的使用.Azure CDN也早在一年前的2015年4月上线了HTTPS加速服务.该加速服务上线一年以来,用户使用量逐渐增 ...
- 利用git pull的勾子实现敏捷部署
监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...
- 剑指Offer——知识点储备-常用算法
剑指Offer--知识点储备-常用算法 快速排序 注:若排序是有序的,采用快排,则退化为冒泡排序. 解决这个问题,采用两个选取基准的方法 (1)随机选取基数(在这个区间内随机取一个数) 出现的恶劣情况 ...
- Hexo 简明教程
概述 对于个人独立博客的搭建,或者一些产品网站的介绍我个人比较推崇直接用静态网站生成器来完成这个事情,对于,静态网页部署方便,浏览速度快. 以下为部分静态网站生成器简要列表: Ruby Jekyll ...