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. 理解活在Iphone中的那些App (四)

    App生存环境之宿主环境 终于开始说一些技术性的话题了,从这里开始的一些技术细节的东西,以前我也没有太刻意的注意过.为了写这个也是刚刚看了一点资料,如果有纰漏,恳请指出. 一个App生存的宿主环境主要 ...

  2. tcp的三次握手:通信的本质:通信通知与信息交换

    tcp的三次握手:通信的本质:通信通知与信息交换

  3. VMware Harbor 学习

    Harbor简介 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distributio ...

  4. Kali-linux使用假冒令牌

    使用假冒令牌可以假冒一个网络中的另一个用户进行各种操作,如提升用户权限.创建用户和组等.令牌包括登录会话的安全信息,如用户身份识别.用户组和用户权限.当一个用户登录Windows系统时,它被给定一个访 ...

  5. javascrict中innerhtml和innerText的关系

    1.time.innerHTML 就是id为time的标签中所有包含的代码内容 比如 <div id='time'><a href='#'>time</a>< ...

  6. 哈希表和字典List和Ilist和array和arraylist的应用

    string x = string.Empty; string y = string.Empty;                Hashtable ht = new Hashtable();     ...

  7. jmeter接口测试3-正则表达式提取器的使用

    正则表达式的用处很多,最基础的用法 1,断言 2,传参(关联) 例子 1.http请求 2正则表达式提取,想要提取列表列中id,一遍打开列表页 如果是1,每次就会取相同的值!匹配数字的权限高于模板$0 ...

  8. 13.56Mhz SI522兼容MFRC522的资料以及对比性能

    (13.56Mhz芯片) SI522是一颗专门替代MFRC522/FM17522,PIN对PIN 完全软硬件兼容.相对于MFRC522,SI522完全替换,不需要做任何更改,同时接受模式下功耗低10m ...

  9. UICollectionViewFlowLayout 的 estimatedItemSize 属性

    这个是collectionView的item 自适应fram的属性, 介绍在网上很多, 但是用法没有太多的举例, 其实这个属性的使用也很简单, 随便给它的不为CGSizeZero的值就好, 但是, 但 ...

  10. ts简单点

    typescript 简洁使用 *做最简洁核心的记录,可以节约时间.再是提炼概括,理解归纳.便于日后查阅联想* > typescript原则之一: 对值所具有的结构进行类型检查 #### 基础类 ...