题目:

iven 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.)

思路:

这道题只要将nums中乘积求出来,然后将乘积除以nums中每一项(nums[i])即可。但要注意0的情况。

  1. /**
  2. * @param {number[]} nums
  3. * @return {number[]}
  4. */
  5. var productExceptSelf = function(nums) {
  6. var pro=1,res=[],flag=0;
  7. for(var i=0,len=nums.length;i<len;i++){
  8. if(nums[i]==0){
  9. flag++;
  10. }else{
  11. pro*=nums[i];
  12. }
  13. }
  14.  
  15. for(var i=0,len=nums.length;i<len;i++){
  16. if(nums[i]!=0&&flag){
  17. res[i]=0;
  18. }else if(nums[i]!=0&&flag==0){
  19. res[i]=pro/nums[i]
  20. }else if(nums[i]==0&&flag==1){
  21. res[i]=pro;
  22. }else if(nums[i]==0&&flag>1){
  23. res[i]=0;
  24. }
  25. }
  26. return res;
  27. };

【数组】Product of Array Except Self的更多相关文章

  1. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

  2. [LintCode] Product of Array Except Self 除本身之外的数组之积

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...

  3. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

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

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

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

  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. C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)

    C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...

  8. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  9. 数组的方法 Array.map();Array.every()和Array.some();数组的indexof();检测是否是数组isArray(obj);

    数组的方法 Array.map(); 栗子: var a=[1,2,,3]; var b=a.map( function(value){return value*value} ); alert(b); ...

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

随机推荐

  1. AE IRasterCursor 获取栅格图层像素值

    在编写使用栅格图层的代码时,常常要获取栅格图层的像素值(PixelValue).如果想获取某一点的像素值,可以使用IRaster2中的getPixelValue方法.但如果想要获得的是图层中的某一块甚 ...

  2. Java中的I/O 线程 网络

    Java学习总结--I/O,线程,网络题目整理 I/O 1.有什么理由必须要用字符流? 答:处理字符数据的语法更方便.自动化字符编码 2.插入哪些代码可以让下面的代码正确编译? Console con ...

  3. hdu 5001 从任意点出发任意走d步不经过某点概率

    http://acm.hdu.edu.cn/showproblem.php?pid=5001 给定n个点m条边的无向图问从任意点出发任意走d步,从不经过某个点的概率 本想先算路过每个点的概率然后用1减 ...

  4. [leetcode] 16. Add Binary

    这个题目相对有点奇怪,题目如下: Given two binary strings, return their sum (also a binary string). For example, a = ...

  5. WP8.1 中获取背景色和主题色

    背景色: Application.Current.RequestedTheme 返回的值是一个枚举,Light 或者 Dark. 主题色: public static Color GetPhoneAc ...

  6. Sql查询两个时间段有重叠的记录

    这个问题看上去简单,但是想了很久,最后发现,自己的思路有点乱. 下面四条时间段都和最上面那个有重叠的关系,就是这么个意思. 这里是问题的答案: 2个时间: a_start, a_end b_start ...

  7. MVC4 项目开发日志(1)

    最近一直在定义一个功能全面,层次结构分明的框架.一边学习一边应用.

  8. Entity Framework学习记录

    记录一次ef code first的学习记录 最近想做一套自己的框架,正在寻找合适的ORM,之前参照力软(很早之前的版本了)的底层代码,做了一套自己的增删改查, 但是使用起来总觉得缺了点什么? 所以决 ...

  9. pageadmin CMS网站制作教程:模板概念解释

    pageadmin CMS网站建设教程:模板概念解释 1.模板页 又叫视图页面,PageAdmin后台栏目或信息中用到的模板页面的统称,格式必须是.cshtml后缀文件,前端人员制作的页面默认都是ht ...

  10. jsp的九大内置对象(查询资料,搬砖所得)

    此笔记大部分摘自大神https://www.cnblogs.com/SimonHu1993/,部分通过网上搜寻补充. 1.Request内置对象(类型:HttpServletRequest    表示 ...