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. js布局库

    1.viz.js The solution was that someone cross compiled Graphviz to Javascript using llvm + emscripten ...

  2. SQLAlchemy总结

    SQL相关操作 创建一个test库 create database test; 授权一个用户 grant all privileges on *.* to 'yangjian'@'%' identif ...

  3. JAVA对象与内存控制

    1.1 实例变量和类变量 成员变量和局部变量: 局部变量分为三大类: 1)形参:在方法签名中定义的局部变量,由方法调用者为其赋值,随方法的结束而消亡. 2)方法内的局部变量:在方法内定义的局部变量,随 ...

  4. JavaBean 规范

    JavaBean是公共Java类,需要满以下条件: 1.所有属性为private2.提供默认无参构造方法3.提供getter和setter4.实现serializable接口 具体为: (1)Java ...

  5. eclipse 自动生成get/set方法

    Shift+Alt+S 会弹出一个对话框 选择Generate Getters and Setters

  6. css实现等高布局 两栏自适应布局 三栏自适应布局

    等高布局: HTML结构如下: <div class="wrapper"> <div class="box"> <h1>.. ...

  7. 程序猿,你为什么须要一台mac?

    用了Mac ,我再也回不去Windows. A:帅哥,我电脑坏了. B:重装系统吧.包好! 重装系统 windows系统解决全部系统问题的一剂神药.Mac 时代再也不须要做这种劳命伤財的事情了,没有什 ...

  8. execute immediate

    首先在这里发发牢骚,指责下那些刻板的书写方式,不考虑读者理不理解,感觉就是给专业人员用来复习用的一样,没有前戏,直接就高潮,实在受不了!没基础或基础差的完全不知道发生了什么,一脸懵逼的看着,一星差评! ...

  9. DBA手记-BBED 的说明

    在10g中连接生成bbed:cd $ORACLE_HOME/rdbms/libmake -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed 11g中缺省未提供BBE ...

  10. Linq 和 SQL的左连接、右连接、内链接

    在我们工作中表连接是很常用的,但常用的有这三种连接方式:左连接.右连接.内链接 在本章节中讲的是1.如何在Linq中使用左连接,右连接,内连接. 2.三种连接之间的特点在哪? 3.Linq的三种连接语 ...