【数组】Product of Array Except Self
题目:
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的更多相关文章
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- [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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【刷题-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 ...
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...
- 【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 ...
- 数组的方法 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); ...
- 【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 ...
随机推荐
- IP之ALTDDIO_in仿真
需要添加altera_mf库,才可以仿真. 上升沿输出,把前一个时钟的数据输出来. `timescale 1 ns/ 1 ns; module altddio_in_ip_tb; reg rst; r ...
- ISO 8895-1
https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout http://czyborra.com/charsets/
- 通过wsdl生成client 的几种方式
wsimport 位置 %JAVA_HOME%/bin/wsimport.exe 帮助 wsimport -help Usage: wsimport [options] <WSDL_URI> ...
- D3 drag
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 日期函数、时间函数总结(MySQL 5.X)
一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函数:now()mysql> select now(); +---------------------+ ...
- Android-LoaderManager异步加载数据库数据
LoaderManager异步加载数据库数据,是在(Activity/fragment/其他UI等) 加载大量的本地Database库表数据,由于数据大在加载过程中会导致UI线程阻塞,导致用户体验不好 ...
- 【转】【译】在 Windows 10 应用程序中注册任意依赖属性的改变
原文地址:http://visuallylocated.com/post/2015/04/01/Registering-to-any-DependencyProperty-changing-in-Wi ...
- rtmp官方标准规范详细解析
标准规范学习: rtmp消息结构,包括几个部分: 时戳:4 byte,单位毫秒.超过最大值后会翻转. 长度:消息负载的长度. 类型ID:Type Id 一部分ID范围用于rtmp的控制信令.还有一部 ...
- 深入学习c++--左值引用和右值引用
#include <iostream> #include <string> #include <vector> using namespace std; int m ...
- Redis的快照
博客链接:http://www.cnblogs.com/zhenghongxin/p/8669913.html redis 本地持久化到硬盘有两种方式,一是快照(snapshotting),二是只追加 ...