238 Product of Array Except Self 除自身以外数组的乘积
一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积。
不用除法 且在O(n)内解决这个问题。
例如,输入 [1,2,3,4],返回 [24,12,8,6]。
进阶:
你可以在常数空间复杂度内解决这个问题吗?(注意:出于空间复杂度分析的目的,输出数组不被视为额外空间。)
详见:https://leetcode.com/problems/product-of-array-except-self/description/
Java实现:
class Solution {
public int[] productExceptSelf(int[] nums) {
int n=nums.length;
int[] f=new int[n];
int[] b=new int[n];
int[] r=new int[n];
Arrays.fill(f,1);
Arrays.fill(b,1);
for(int i=0;i<n-1;++i){
f[i+1]=f[i]*nums[i];
}
for(int i=n-1;i>0;--i){
b[i-1]=b[i]*nums[i];
}
for(int i=0;i<n;++i){
r[i]=f[i]*b[i];
}
return r;
}
}
C++实现:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
vector<int> f(n,1),b(n,1),res(n);
for(int i=0;i<n-1;++i)
{
f[i+1]=f[i]*nums[i];
}
for(int i=n-1;i>0;--i)
{
b[i-1]=b[i]*nums[i];
}
for(int i=0;i<n;++i)
{
res[i]=f[i]*b[i];
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4650187.html
238 Product of Array Except Self 除自身以外数组的乘积的更多相关文章
- 238. Product of Array Except Self除自身以外数组的乘积
网址:https://leetcode.com/problems/product-of-array-except-self/ 参考:https://leetcode.com/problems/prod ...
- Leetcode238. Product of Array Except Self除自身以外数组的乘积
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- 【刷题-LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
随机推荐
- codeforces 691F(组合数计算)
Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to ...
- I/O---BufferedInputStream及相关类介绍
关于BufferedInputStream 是java提供的具有缓存作用的字节输入流.与之对应的还有BufferedOutStream 和 BufferedRead 和BufferedWriter 这 ...
- IE11 文档模式空白
环境描述: win7 64位系统 安装了 更新 IE11-Windows6.1-KB2929437-x64.IE11-Windows6.1-KB3008923-x64 解决方案: 卸载 IE11-Wi ...
- Android GIS开发系列-- 入门季(9) 定位当前的位置
利用MapView定位当前的位置 这里要用到Arcgis中的LocationDisplayManager这个类,由于比较简单.直接上代码: LocationDisplayManager locatio ...
- Eclipse的SVN插件 Subclipse
原文:https://www.oschina.net/p/subclipse Subclipse 是一个为 Eclipse IDE 添加 Subversion 支持的项目.支持几乎所有版本的Eclip ...
- 2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)
题目大意: 有K堆石子,每堆有Ki个.两人的操作能够是: 1 从某一堆拿走一个 假设该堆在此之后没有石子了.就消失 2 合并两个堆 求是否 ...
- Java之旅hibernate(2)——文件夹结构
Hibernate的jar最好是到官网上进行下载.下载最新的稳定的版本号.之后进行解压,以下我们介绍一下hibernate的包结构. 1. 包结构 我们能够看到包文件夹结构发生了变化.我以5 ...
- 智能社区--HI3516C可视门禁研发出来咯
铝壳.非常大气的外壳. 200W像素,HI3516C,携带server.创新的产品.欢迎交流:QQ237753582 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...
- POJ 2299 Ultra-QuickSort(线段树+离散化)
题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...
- 【hdu3544】 Alice's Game
给一块n*m的巧克力,Alice只能垂直切,切成A*m和B*m,并且A+B=n,Bob只能横切,只能切成A*n和B*n,并且A+B=m. 对于n*n的这种巧克力,谁先切了第一刀,就直接让对方有切两刀的 ...