238. Product of Array Except Self

 
 
Total Accepted: 41565 Total Submissions: 97898 Difficulty: Medium

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)和递归又有了新的理解)的更多相关文章

  1. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  2. 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 ...

  3. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  4. 【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 ...

  5. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  6. 【刷题-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 ...

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

  8. (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 ...

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

随机推荐

  1. jenkins连接提示错误urllib.error.HTTPError: HTTP Error 403

    昨天在执行python连接Jenkins获取编译失败日志失败时,出现错误,具体报错如下,主要是在连接问题上的问题,做了一个请求 就提示错误 原因在于Jenkins的权限,或者访问页面的url需要进行登 ...

  2. P3174 [HAOI2009]毛毛虫

    题目描述 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一个毛毛虫,点数越多,毛毛虫就越大.例如下图左边的树(图 1 )抽出一部分就变成了右边的一个毛毛虫了(图 2 ). 输入输出格 ...

  3. POJ3907 Build Your Home

    嘟嘟嘟 题意:按逆时针或顺时针给出一个多边形,求面积. 解法:直接套用公式:\(S = \frac{1}{2}|\sum _ {i = 1} ^ {n} {v_i \times v_{i + 1}}| ...

  4. virtualbox+vagrant学习-4-Vagrantfile-9-Vagrant Settings

    Vagrant Settings 配置命名空间:config.vagrant config.vagrant配置将修改vagrant本身的行为 Available Settings可用设置 config ...

  5. Mybatis 插件

    在spring整合mybatis的配置中, 插件注册 <property name="plugins"> <!-- 配置SqlSessionFactoryBean ...

  6. Java Calendar and SimpleDateFormat 时间模块

    package UtilTest; import java.util.Calendar; import java.text.SimpleDateFormat; import org.apache.co ...

  7. Loadrunner之HTTP接口测试

    Loadrunner之HTTP接口测试 接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文这一个过程. ...

  8. MFC文档应用程序CToolBar:设置两个工具条并列停靠到同一条边上 转

    转自:http://blog.csdn.net/panshiqu/article/details/9369891# 将多个工具条同时并列停靠在某窗口的某一条边上.对于这种停靠方法,利用上述工具条控制函 ...

  9. linux内核追踪(trace)(QEMU+gdb)

    1.引言 Linux内核是一个很大的模块,如果只是看源码有时会难以理解Linux内核的一些代码设计情况,如果可以结合Linux内核运行同时阅读源码再好不过,本文大致介绍Linux内核追踪方式,采用工具 ...

  10. Linux Shell常用技巧(十二)

    二十三. Bash Shell编程:  1.  读取用户变量:    read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入.在read命令后面,如果 ...