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. 2019.2.27 Eclipse中的Tomcat设置Tomcat服务器手动重启

    1.打开Tomcat的设置界面 2.找到Modules界面 3.去掉,就改为手动了

  2. Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing

    SIGCOMM17 摘要 在现有的网络测量任务中包括流量监测.数据收集和一系列网络攻击的预防.现有的基于sketch的测量算法存在严重性能损失.大量计算开销以及测量的精确性不足,而基于硬件的优化方法并 ...

  3. Hibernate工作原理及为什么要用?. Struts工作机制?为什么要使用Struts? spring工作机制及为什么要用?

    三大框架是用来开发web应用程序中使用的.Struts:基于MVC的充当了其中的试图层和控制器Hibernate:做持久化的,对JDBC轻量级的封装,使得我们能过面向对象的操作数据库Spring: 采 ...

  4. Angular动态表单生成(一)

    好久不写博客了,手都生了,趁着最近老大让我研究动态表单生成的时机,撸一发博客~~ 开源项目比较 老大丢给我了两个比较不错的开源的动态表单生成工具,这两个项目在github上的star数量基本持平: h ...

  5. Linux 内核版本号查看

    简要:1,lsb_release -a 查看linux系统版本 2,uname -a 查看内核版本

  6. wordpress安装(ubuntu+nginx+php+mariadb)

    一.   环境 ubuntu12.04.4 nginx 1.6.0 mariadb 10.0 更新系统补丁 sudo apt-get update sudo apt-get dist-upgrade ...

  7. vue中通过定义的数组循环将img的src引入图片却不显示图片问题解决方法

    需要前端循环图片数组将其放到页面中去. 需要将src渲染到页面中,如果单纯写src的路径会出现不显示图片的问题 因为图片路径在assets,所以需要require一下.

  8. 图形剖析,当给 ul 设置padding=0, margin=0后 li前面的小黑点消失的现象原理!

  9. oracle数据库的配置文件

    url=jdbc:oracle:thin:@localhost:1521:orcldriver=oracle.jdbc.OracleDriverusrname=GJQ   (PLSQL Develop ...

  10. vim内替换文件内容

    几个常用的方法如下: :%s/foo/bar/g 把全部foo替换为bar,全局替换 :s/foo/bar/g 当前行替换foo为bar :%s/foo/bar/gc 替换每个foo为bar,但需要确 ...