52.Product of Array Except Self(除过自身的数组乘积)
Level:
Medium
题目描述:
Given an array nums
of n integers where n > 1, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
思路分析:
我们可以求出整个数组的乘积allProduct,然后,result[ i ]就为allProduct除以nums[ i ],如果数组中有零,那么除过为零元素对应位置外,其他位置对应结果都为零。
代码:
public class Solution{
public int []productExceptSelf(int[]nums){
int allProduct=1;
int[] res=new int[nums.length];
int flag=0; //标志出现零
for(int i=0;i<nums.length;i++){
if(nums[i]==0&&flag==0){
flag=1;
continue;
}
allProduct=allProduct*nums[i];
}
for(int i=0;i<res.length;i++){
if(flag==1){
if(nums[i]!=0)
res[i]=0;
else
res[i]=allProduct;
}else{
res[i]=allProduct/nums[i];
}
}
return res;
}
}
52.Product of Array Except Self(除过自身的数组乘积)的更多相关文章
- [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 ...
- [LeetCode] 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 ...
- LeetCode 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 ...
- [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 ...
- [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 ...
- 【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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
随机推荐
- [转载]Redux原理(一):Store实现分析
写在前面 写React也有段时间了,一直也是用Redux管理数据流,最近正好有时间分析下源码,一方面希望对Redux有一些理论上的认识:另一方面也学习下框架编程的思维方式. Redux如何管理stat ...
- webpack的理解、总结
weabpck的基础应用 https://blog.zhangjd.me/2016/06/19/webpack-your-bags/ https://juejin.im/post/5cc26dfef2 ...
- 2018-2-13-C#-复制列表
title author date CreateTime categories C# 复制列表 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 +0 ...
- quotaon - 开启关闭文件系统配额
总览 (SYNOPSIS) quotaon [ -e | d ] [ -vug ] filesystem... quotaon [ -e | d ] [ -avug ] quotaoff [ -e | ...
- ResourceBundle读取配置文件
import java.util.ResourceBundle; /** * Created by win7 on 2017/5/20. */public class Test1 { public s ...
- Struts2之param标签的使用
struts2的s:param标签主要有两个属性name与value: 传值 若想在value属性中输入字符串,则可以这样写:<s:param name="tableTitle&quo ...
- js中给数组添加元素的方法有哪些
unshift:将参数添加到原数组开头,并返回数组的长度 pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined push:将参数添加到原数组末尾,并返回数组的长度 co ...
- js-放大镜效果
jd或者淘宝的具体商品有个放大镜的效果.虽然网上类似插件琳琅满目,应用到项目上有诸多不便,自己抽点时间自己写了个类似插件,积累下代码,何乐而不为呢!!let‘go: 打算把此特效封装成个插件,先把最基 ...
- Linux系统重要文件(二)
Linux系统重要文件概述 一系统自动挂载文件 文件路径信息:/etc/fstab文件作用说明:实现存储设备自动挂载 [root@centos7 ~]# cat /etc/fstab # # /etc ...
- python学习笔记(一)python简介和基础
1.什么是python? python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学.1989年诞生,Guido(龟叔)开发. 编译型语言:代码在编译之后,编译成2进制的文件 ...