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. git修改远程仓库

    三种方式都可以. 1. 修改命令 git remte origin set-url URL 2.先删后加 git remote rm origin git remote add origin git@ ...

  2. html5和CSS3中新特性

    H5 1.语义化标签 header.footer.aside.nav.section.article 2.表单输入类型 email.url.number.range.Date Pickers.sear ...

  3. LVS (Linux Virtual Server) 思维导图笔记

  4. Dubbo实践(三)框架设计

    整体设计 图例说明: 图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口,位于中轴线上的为双方都用到的接口: 图中从下至上分为十层,各层均为单向依赖,右边的黑色箭头代表层 ...

  5. 通讯协议(一)HTTP协议

    协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器.目前我们使 ...

  6. Loadrunner测试webservice协议总结

    Loadrunner测试webservice协议总结 一.协议选择 1.打开Virtual user generator,新建脚本,选择webservice协议

  7. HDU 3047 Zjnu Stadium(带权并查集,难想到)

    M - Zjnu Stadium Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  8. C#中枚举

    1.枚举是一组命名整形常量,枚举类型使用Enum关键字进行声明的.在C#中枚举是值数据类型,枚举包含自己的值,且不能继承或传递继承.

  9. SpringMVC中controller的几种返回值

    String :跳转到对应的返回值中. return “/index”: ModelAndView: 控制页面跳转方式: 1. ModelAndView modelAndView = new Mode ...

  10. 解决Js跨域访问的问题

    1,最近有个需求,用Js获取Html标签<input type="file"/>的路径!遇到代码拒绝访问,提示安全验证,不允许跨域访问,简单的设置一下浏览器即可,不过对 ...