题目:

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的情况。

/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var pro=1,res=[],flag=0;
for(var i=0,len=nums.length;i<len;i++){
if(nums[i]==0){
flag++;
}else{
pro*=nums[i];
}
} for(var i=0,len=nums.length;i<len;i++){
if(nums[i]!=0&&flag){
res[i]=0;
}else if(nums[i]!=0&&flag==0){
res[i]=pro/nums[i]
}else if(nums[i]==0&&flag==1){
res[i]=pro;
}else if(nums[i]==0&&flag>1){
res[i]=0;
}
}
return res;
};

【数组】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. 继承方法-->call继承

    function Person(name,age,sex){ this.name = name; this.age = age; this.sex = sex; }function P1(name,a ...

  2. SecureCRT和乱码

    示例: # ls /usr/local/r3c/bin/lib /bin/ls: /usr/local/r3c/bin/lib: ????????? 查看系统字符集设置: # locale LANG= ...

  3. Java子父类中的构造函数实例化过程

    其实我们发现子类继承父类操作很简单,如果要是去深入的研究下会发现,实例化过程并非是我们看到的那样,我们就以代码举例来说明: 问大家,以下代码执行会输出什么呢? package com.oop; /** ...

  4. 老码农冒死揭开行业黑幕:如何编写无法维护的代码[ZZ]

    下面是一篇有意思的"代码大全",可谓 逆软件工程. 老码农冒死揭开行业黑幕:如何编写无法维护的代码 原文如下 让自己稳拿铁饭碗 ;-) – Roedy Green(翻译版略有删节) ...

  5. 【算法34】蓄水池抽样算法 (Reservoir Sampling Algorithm)

    蓄水池抽样算法简介 蓄水池抽样算法随机算法的一种,用来从 N 个样本中随机选择 K 个样本,其中 N 非常大(以至于 N 个样本不能同时放入内存)或者 N 是一个未知数.其时间复杂度为 O(N),包含 ...

  6. .NET高级代码审计(第一课)XmlSerializer反序列化漏洞

    0X00 前言 在.NET 框架中的 XmlSerializer 类是一种很棒的工具,它是将高度结构化的 XML 数据映射为 .NET 对象.XmlSerializer类在程序中通过单个 API 调用 ...

  7. datename和datepart

    select datename(year, getdate()) + 'aaa11' --不报错 datename返回的是nvarchar类型 select datalength(datename(y ...

  8. HttpWebRequest 模拟浏览器访问网站

    最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...

  9. 编译Bootstrap,定制自己的模板

    完全不懂LESS,也懒的去学习它,凭多年的经验,感觉也不用专门花时间去学习了.反正它应该是很成熟的,能执行即可.我用的是WIN7,为了定制颜色等各种特性,需要重新编译Bootstrap.在网上到处中, ...

  10. C#之使用CefSharp创建客户端

    安装NuGet包 在Visio studio中右击解决方案,选择管理NuGet包,搜索安装CefSharp.WinForms. 配置工作 (1)首先右击项目选择属性,在"生成"选项 ...