238. Product of Array Except Self(对O(n)和递归又有了新的理解)
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 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.)
Subscribe to see which companies asked this question
Code:
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
int i,tmp = 1;
int *arr = (int *)malloc(sizeof(int)*(numsSize));
arr[numsSize-1] = 1;
for(i = numsSize-2;i>=0;i--)
arr[i] = arr[i+1]*nums[i+1];
for(i = 0;i<numsSize;i++)\
{
arr[i] = tmp*arr[i];
tmp*=nums[i];
}
*returnSize = numsSize;
return arr;
}
//一个嵌套把前边的乘积之和带过来,再在跳出嵌套之前得出相应的结果。
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
multiply(nums, 1, 0, numsSize);
*returnSize = numsSize;
return nums;
}
int multiply(int *a, int fwdProduct, int indx, int N) {
int revProduct = 1;
if (indx < N) {
revProduct = multiply(a, fwdProduct * a[indx], indx + 1, N);
int cur = a[indx];
a[indx] = fwdProduct * revProduct;
revProduct *= cur;
}
return revProduct;
}
238. Product of Array Except Self(对O(n)和递归又有了新的理解)的更多相关文章
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 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 ...
- [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 ...
- (Skill)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 ...
- 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 ...
随机推荐
- 用 Visual Studio 2012 调试你的ASP程序
最近搞到一段很值得参考的ASP项目,无奈技术有限,打开看完代码后感觉自己就像从来没学过ASP一样.唉...大神的世界 不过在网上看到一个有趣的方法,可以用Visual Studio 2005来调试AS ...
- BZOJ 1012 最大数maxnumber 线段树
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1012 题目大意: 见链接 思路: 直接用线段树模拟一下就可以了. #include&l ...
- keepalived.md
配置文件说明 global_defs区域 global_defs { notification_email { acassen@firewall.loc failover@firewall.loc s ...
- 协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)
网络层(network layer)是实现互联网的最重要的一层.正是在网络层面上,各个局域网根据IP协议相互连接,最终构成覆盖全球的Internet.更高层的协议,无论是TCP还是UDP,必须通过网络 ...
- 用Qt制作的Android独立游戏《吃药了》公布
一个多月的努力最终有了回报,我自己研究制作的独立游戏<吃药了>.最终在360应用商店上线了. 这一款游戏呢.使用的是Qt开发的.事实上开发这款简单的应用之前.我 ...
- BZOJ3997:[TJOI2015]组合数学(DP,Dilworth定理)
Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一 ...
- SSM衍生的配置文件
JDBC:(Java database connectivity) 目的:将Java语言和数据库解耦和,使得一套Java程序能对应不同的数据库. 方法:sun公司制定了一套连接数据库的接口(API). ...
- oracle中如何将表缓存到内存中
oracle快速将表缓存到内存中,使得访问速度加快. 共有2种方法: 1)alter table fisher cache; 2)alter table fisher storage(buffer ...
- FMDB数据库使用
创建数据库路径 NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainM ...
- canvas转img,blob相互转换
摘自:https://www.cnblogs.com/jyuf/p/7251591.html 函数都比较简单,直接看就ok了 /*----------------------------------- ...