描述

给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。

示例:

输入: [1,2,3,4]
输出: [24,12,8,6]
说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。

进阶:
你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)

解析

先正向遍历一遍数组,保存元素左边所有元素的乘积;再反向遍历一次,算出元素右边所有元素的乘积。即可得结果。

代码

 public int[] productExceptSelf(int[] nums) {
if (null == nums || nums.length <= 0) {
return null;
}
int[] res = new int[nums.length];
int k = 1;
for (int i = 0; i < nums.length; i++) {
res[i] = k;
k *= nums[i];
}
k = 1;
for (int i = nums.length - 1; i >= 0; i--) {
res[i] *= k;
k *= nums[i];
}
return res;
}
}

[LeetCode] 238. 除自身以外数组的乘积 ☆☆☆(左积*右积)的更多相关文章

  1. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

  2. Java实现 LeetCode 238 除自身以外数组的乘积

    238. 除自身以外数组的乘积 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元 ...

  3. [leetcode]238. 除自身以外数组的乘积

    题目描述 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输 ...

  4. LeetCode 238. 除自身以外数组的乘积( Product of Array Except Self)

    题目描述 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输 ...

  5. leetcode 238. 除自身以外数组的乘积 (python)

    给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...

  6. Leetcode题目238.除自身以外数组的乘积(中等)

    题目描述: 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: ...

  7. leecode 238除自身以外数组的乘积

    class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { //用除法必须要 ...

  8. 剑指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] * ...

  9. [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 ...

随机推荐

  1. Oracle OLAP

    w 国产商业智能 BI 这 20 年(1997-2017)| 天善智能  http://mp.weixin.qq.com/s/VJURX2qhmL0Uj1dctMyEig 1997-2001年,萌芽阶 ...

  2. JSON基础,简单介绍

    JSON(JavaScript Object Notation(记号.标记)) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第5节 线程池_1_线程池的概念和原理

    线程的底层原理 集合有很多种,线程池的集合用LinkedList最好

  4. create-react-app 创建react应用环境变量(env)配置

    参考:https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables What other . ...

  5. python 正则表达式 re.sub & re.subn

    Grammar: re.sub(pattern, repl, string[, count]) 使用repl替换string中每一个匹配的子串后返回替换后的字符串.当repl是一个字符串时,可以使用\ ...

  6. 获取文件夹中前N个文件

    @echo off set input="list.txt" set srcDir="%1" set /a fileCount=10 set /a curInd ...

  7. oracle--groupby分组学习

    使用group by分组 在多行函数中不能直接使用普通字段,除非group by 在多行函数中不能直接使用单行函数,除非group by group by学习: ---1.使用group by进行数据 ...

  8. 如何通过脚本实现显示版本号、CPU、硬盘和内存条大小

    COLOR="\033[1;$[RANDOM%7+31]m"     COLOR1="\033[1;$[RANDOM%7+31]m"COLOR2="\ ...

  9. winCE 获取路径信息

    最近在做一个SAP的winCE扫描枪项目,采用C#开发,不过在获取路径是采用了常用的System.IO.Directory.GetCurrentDirectory, 并不能使用:查询后了解到winCE ...

  10. SQL SERVER SP命令及实现跨数据库查询

    1.数据库: (1)sp_helpdb:报告有关指定数据库或所有数据库的信息. 例:sp_helpdb   --显示所有数据库信息(名称.大小等) 例:sp_helpdb Recruitment   ...