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 ...
随机推荐
- Java面试基本知识
Java基本知识 基本知识 服务器:Tomcat 支持Servlet jsp JBoss 开源应用服务器 Apache:最广泛的http服务器,只支持静态网页 String是长度不可变,用+=的时候会 ...
- asp,mdb,工具
<%set conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLE ...
- P2059 [JLOI2013]卡牌游戏
题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先把 ...
- 线性回归 Python实现
import numpy as np import pylab def plot_data(data, b, m): x = data[:, 0] y = data[:, 1] y_predict = ...
- 好用的纯CSS加载动画-spinkit
首先放一个css spinkit <style> .loaders{ width: 100%; height: 100%; padding: 100px; box-sizing: bor ...
- Kali-linux分析密码
在实现密码破解之前,介绍一下如何分析密码.分析密码的目的是,通过从目标系统.组织中收集信息来获得一个较小的密码字典.本节将介绍使用Ettercap工具或MSFCONSOLE来分析密码. 8.2.1 E ...
- js中时间的操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- csv文件的使用,csv空白行问题
首先w+和wb区别 两者都是用于以只写方式打开指定文件指定文件原来不存在,则在打开时由系统新建一个以指定文件名命名的文件,如果原来已存在一个以该文件名命名的文件,则在打开时将该文件删去,然后重新建立一 ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- initUrl方法
private String initUrl(String preurl,String taskurl) { if(JavaUtil.match(taskurl, "/[a-z]+$&quo ...