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 ...
随机推荐
- webstorm中导入git项目
1.打开webStrom 配置git File–setting
- python虚拟环境virtualenv高级篇
我曾经写过一篇virtualenv的博客:http://www.cnblogs.com/anpengapple/p/5907416.html 总体来讲还是适用的,不过稍微傻了一点.这一篇的内容有两个: ...
- ARM汇编之MOV指令
http://blog.csdn.net/lsywk/article/details/8799837 一.指令格式 MOV{条件}{S} 目的寄存器,源操作数 二.指令详解 MOV指令可完成从另一个 ...
- shuf 按行打乱文本命令
长选项必须使用的参数对于短选项时也是必需使用的. -e, --echo 将每个参数视为输入行 -i, --input-range=LO-HI 将LO 到HI 的每个数字视为输入行 -n, --head ...
- 【jQuery】动画小练习
1.jQuery部分代码如下 <script type="text/javascript"> $(function(){ var page = 1; var i = 4 ...
- Kali-linux免杀Payload生成工具Veil
Veil是一款利用Metasploit框架生成相兼容的Payload工具,并且在大多数网络环境中能绕过常见的杀毒软件.本节将介绍Veil工具的安装及使用. 在Kali Linux中,默认没有安装Vei ...
- Windows安装openssl
Windows下有两种方式安装openssl,第一种是采用安装包方式进行安装,第二种是采用编译源码方式进行安装.这里采用第一种方式,简单,直接. windows的openssl安装包的下载地址为:ht ...
- PAT——1061. 判断题
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...
- 【51nod 1685】 第K大区间2
题目描述: 定义一个长度为奇数的区间的值为其所包含的的元素的中位数.现给出n个数,求将所有长度为奇数的区间的值排序后,第K大的值为多少. 样例解释: [l,r]表示区间的值 [1]:3 [2]:1 [ ...
- Android-社会化分享
2016年2月25日下午3点:现在无事,整理下这两天在网上搜集到的乱起八糟的东西和我的思路. 关于对社会化分享的了解,源于前天的一次apk打包操作.现在的情况是:开发编写功能代码提交SVN,我把代码d ...