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(除过自身的数组乘积)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

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

  7. 【08_238】Product of Array Except Self

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

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

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

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

随机推荐

  1. 本机ip地址怎么查

     转自:https://www.192ly.com/basic/local-ip-address-lookup-method.html 百度搜索一下[IP],你就可以轻松看到你的IP地址了,百度出来的 ...

  2. RabbitMQ基于Stomp实现与MQTT客户端通信

    请参照RabbitMQ应用和SpringBoot集成RabbitMQ并实现消息确认机制 详情参照官方文档https://www.rabbitmq.com/stomp.html.https://gith ...

  3. 【linux 进程杀死】批量杀死进程

    一次杀死包含 api_antispan 开头的的所有进程 https://blog.csdn.net/u013421629/article/details/83512498

  4. 一、UC中文调试

    一.只支持UC浏览器,版本号6.2.3831.3,复制resources.pak到UC安装目录下,覆盖同名文件即可

  5. 在vCenter上创建新用户 (适用版本6.0)

  6. 2.VUE前端框架学习记录二

    VUE前端框架学习记录二:Vue核心基础2(完结)文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baid ...

  7. 第04章 AOP概述

    第04章 AOP概述 1.AOP概述 ●AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统 OOP(Object-Oriented Progra ...

  8. IBM研究人员开发了一对低功耗,高性能的计算机视觉系统

    机器学习算法近年来有了突飞猛进的发展.例如,像Facebook这样的最先进的系统,可以在一小时内训练图像分类算法,而不会牺牲准确性.但是,许多这些系统都是在具有强大GPU的高端机器上进行培训的,随着物 ...

  9. 提高wifi速度的设置办法

    系列的提高wifi速度的设置办法 在DNS一栏有你们家的地址,在你们家的地址前输入“114.114.114.114”并以“,”结尾(注意:要用英文输入法哦.) 设置完后点击左上角的“无线局域网”回到初 ...

  10. 回顾Servlet及SpringMVC

    什么是Servlet? Servlet是运行在Web服务器或应用服务器上的程序,它是作为来自Web浏览器或其他HTTP客户端的请求和HTTP服务器上的数据库 或应用程序之间的中间层. servlet架 ...