[LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array nums
of n integers where n > 1, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Example:
- Input:
[1,2,3,4]
- Output:
[24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
这道题给定我们一个数组,让我们返回一个新数组,对于每一个位置上的数是其他位置上数的乘积,并且限定了时间复杂度 O(n),并且不让我们用除法。如果让用除法的话,那这道题就应该属于 Easy,因为可以先遍历一遍数组求出所有数字之积,然后除以对应位置的上的数字。但是这道题禁止我们使用除法,那么我们只能另辟蹊径。我们想,对于某一个数字,如果我们知道其前面所有数字的乘积,同时也知道后面所有的数乘积,那么二者相乘就是我们要的结果,所以我们只要分别创建出这两个数组即可,分别从数组的两个方向遍历就可以分别创建出乘积累积数组。参见代码如下:
C++ 解法一:
- class Solution {
- public:
- vector<int> productExceptSelf(vector<int>& nums) {
- int n = nums.size();
- vector<int> fwd(n, ), bwd(n, ), res(n);
- for (int i = ; i < n - ; ++i) {
- fwd[i + ] = fwd[i] * nums[i];
- }
- for (int i = n - ; i > ; --i) {
- bwd[i - ] = bwd[i] * nums[i];
- }
- for (int i = ; i < n; ++i) {
- res[i] = fwd[i] * bwd[i];
- }
- return res;
- }
- };
Java 解法一:
- public class Solution {
- public int[] productExceptSelf(int[] nums) {
- int n = nums.length;
- int[] res = new int[n];
- int[] fwd = new int[n], bwd = new int[n];
- fwd[0] = 1; bwd[n - 1] = 1;
- for (int i = 1; i < n; ++i) {
- fwd[i] = fwd[i - 1] * nums[i - 1];
- }
- for (int i = n - 2; i >= 0; --i) {
- bwd[i] = bwd[i + 1] * nums[i + 1];
- }
- for (int i = 0; i < n; ++i) {
- res[i] = fwd[i] * bwd[i];
- }
- return res;
- }
- }
我们可以对上面的方法进行空间上的优化,由于最终的结果都是要乘到结果 res 中,所以可以不用单独的数组来保存乘积,而是直接累积到结果 res 中,我们先从前面遍历一遍,将乘积的累积存入结果 res 中,然后从后面开始遍历,用到一个临时变量 right,初始化为1,然后每次不断累积,最终得到正确结果,参见代码如下:
C++ 解法二:
- class Solution {
- public:
- vector<int> productExceptSelf(vector<int>& nums) {
- vector<int> res(nums.size(), );
- for (int i = ; i < nums.size(); ++i) {
- res[i] = res[i - ] * nums[i - ];
- }
- int right = ;
- for (int i = nums.size() - ; i >= ; --i) {
- res[i] *= right;
- right *= nums[i];
- }
- return res;
- }
- };
Java 解法二:
- public class Solution {
- public int[] productExceptSelf(int[] nums) {
- int n = nums.length, right = 1;
- int[] res = new int[n];
- res[0] = 1;
- for (int i = 1; i < n; ++i) {
- res[i] = res[i - 1] * nums[i - 1];
- }
- for (int i = n - 1; i >= 0; --i) {
- res[i] *= right;
- right *= nums[i];
- }
- return res;
- }
- }
Github 同步地址:
https://github.com/grandyang/leetcode/issues/238
类似题目:
参考资料:
https://leetcode.com/problems/product-of-array-except-self/
https://leetcode.com/problems/product-of-array-except-self/discuss/65638/My-simple-Java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Product of Array Except Self 除本身之外的数组之积的更多相关文章
- [LintCode] Product of Array Except Self 除本身之外的数组之积
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...
- [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 ...
- 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 Product of Array Except Self (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- [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 ...
- LeetCode——Product of Array Except Self
Description: Given an array of n integers where n > 1, nums, return an array output such that out ...
- 238. [LeetCode] 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 ...
- LeetCode -- Product of Array Except Self My Submissions Question
Question: Given an array of n integers where n > 1, nums, return an array output such that output ...
- LeetCode: Product of Array Except Self
Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...
随机推荐
- 读书笔记--SQL必知必会02--检索数据
2.1 SELECT语句 SELECT语句的用途是从一个或多个表中检索信息. 关键字(keyword) 作为SQL组成部分的保留字.关键字不能作为表或列的名字. 2.2 检索单个列 多条SQL语句必须 ...
- Three.js 第一篇:绘制一个静态的3D球体
第一篇就画一个球体吧 首先我们知道Three.js其实是一个3D的JS引擎,其中的强大之处就在于这个JS框架并不是依托于JQUERY来写的.那么,我们在写这一篇绘制3D球体的文章的时候,应该注意哪些地 ...
- Windows Programming ---- Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- go语言赋值
使用赋值语句可以更新一个变量的值,最简单的赋值语句是将要被赋值的变量放在=的左边,新值的表达式放在=的右边. x = // 命名变量的赋值 *p = true // 通过指针间接赋值 person.n ...
- css水平居中的各种方法
说到水平居中,大家可能觉得很简单啊,text-align:center 就OK了. 但是,有时候会发现这样写了也没出效果.原因是什么呢? 请往下看. 水平居中:分为块级元素居中和行元素居中 行内元素 ...
- SIMLock锁卡功能解析
一.锁卡背景介绍 锁卡即SIMLock,当手机开机启动或者插入SIM卡时,手机modem侧预置在NV项中的配置信息会与SIM卡中的信息做比对,检测是否匹配.若匹配,则SIM卡可以正常使用.若不匹配,则 ...
- spring源码:IOC(li)
一.BeanDefinition BeanDefinition是配置文件<bean>元素标签在容器中内部表示形式.创建最终的BeanDefinition主要包括两个步骤: 1)利用Bean ...
- getRequestDispatcher()与sendRedirect()的区别
1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ; response.sendRedirect()是重新定向,前后页面不是一个request ...
- Flash Professional 报错 TypeError: Error #1034: 强制转换类型失败:无法将 xxxx@zzzz 转换为 yyy
通常是因为xxx yyy 两个不同链接名的元件 使用了同一个属性名
- Java中一些常用的方法
1.计算程序运行时常 long start = System.currentTimeMillis(); … … … long end = System.currentTimeMillis(); Sys ...