Question:

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

Analysis:

给出有n个元素的整数数组(n > 1) nums, 返回一个数组输出,其中output[i]为除了nums[i]外其它个元素的乘积。不要分割数组并且在O(n)的时间内解决这个问题。

例如给出数组:[1, 2, 3, 4],返回[24, 12, 8, 6].

注意:

你能在常数空间复杂度内解决这个问题吗?(在空间复杂度的计算中,output数组不计算在内)

分析:

在计算乘除时,0是一个特殊元素,还要考虑正负号的问题。因此我们的思路是:

a. 一般情况下(无0元素):为避免溢出,用一个long型的参数存储所有元素的乘积,然后循环数组一次,依次除以当前元素的值,将除数保存到output数组中即可;

b. 若数组中含有0元素,但是我们不知道0元素的个数有多少,因此需要用另外一个参数zero对零元素计数。如果数组中仅含一个0,则只有0元素的位置为其他所有元素的乘积,其他的元素都为0;如果数组中含有多余一个0,则所有位置都为0.

本题很简单,只要按照特殊元素0分类即可。

Answer:

public class Solution {
public int[] productExceptSelf(int[] nums) {
long temp = 1;
int zero = 0;
int[] result = new int[nums.length];
for(int x : nums) {
if(x == 0)
zero++;
else temp *= x;
} if(zero > 1)
return result;
else if(zero == 1){
for(int i=0; i<nums.length; i++) {
if(nums[i] == 0)
result[i] = (int) (temp);
}
return result;
}
else {
for(int i=0; i<nums.length; i++) {
if(nums[i] == 0)
result[i] = (int) (temp / nums[i]);
result[i] = (int) (temp / nums[i]);
}
return result;
} }
}

LeetCode -- Product of Array Except Self My Submissions Question的更多相关文章

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

  2. LeetCode——Product of Array Except Self

    Description: Given an array of n integers where n > 1, nums, return an array output such that out ...

  3. 238. [LeetCode] 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 ...

  4. LeetCode: Product of Array Except Self

    Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...

  5. LeetCode Product of Array Except Self (除自身外序列之积)

    题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...

  6. [LeetCode] 11. Container With Most Water My Submissions Question 解题思路

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

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

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

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

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

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

随机推荐

  1. JDK1.8简单配置环境变量---两步曲

    鄙人最近重新装完系统之后,在安装和配置jdk1.8的时候,发现网上许多教程配置jdk环境变量时都还在沿用传统的方式配置,但是随着技术的更新,完全没有必要那么麻烦了. 下载和安装jdk的教程,在这里就不 ...

  2. java从图片中识别文字

    package com.dream.common; import java.awt.image.BufferedImage; import java.io.File; import java.io.I ...

  3. 开发工具cfree安装报错解决

    报错如下: [ --------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW-------------------- 检查文件依赖性... 正在编译 ...

  4. 【JavaScript】jQuery绑定事件

    jquery中直接绑定事件:只能用在程序中一开始就存在的html代码 目标元素.click(function(){ }) jquery中间接绑定事件: 如果目标元素是js生成的,则需要间接绑定事件,用 ...

  5. c语言可变参数函数

    c语言支持可变参数函数.这里的可变指,函数的参数个数可变. 其原理是,一般情况下,函数参数传递时,其压栈顺序是从右向左,栈在虚拟内存中的增长方向是从上往下.所以,对于一个函数调用 func(int a ...

  6. poj 3685 矩阵问题 查找第K小的值

    题意:N阶矩阵Aij= i2 + 100000 × i + j2 – 100000 × j + i × j,求第M小的元素. 思路:双重二分 考虑到,aij是跟着i递增的,所以i可以作为一个二分搜索 ...

  7. Android 浮动按钮+上滑隐藏按钮+下滑显示按钮

    1.效果演示 1.1.关注这个红色的浮动按钮 . 可以看到,上滑的时候浮动按钮消失,因为用户迫切想知道下面的东西,而不是回到顶部. 当下滑的时候,用户想回到原来的位置,就可以点击浮动按钮,快速回到顶部 ...

  8. PHP代码审计3-SQL注入,CSRF,动态函数执行与匿名函数执行,unserialize 反序列化漏洞,变量覆盖,文件管理,文件上传

    SQL注入 审计语句 [输入参数] SELECT,DELETE,UPDATE,INSERT 防御 转义: 1.开启gpc:判断解析用户提示的数据 2.mysql_real_escape_string( ...

  9. MAC中mongodb的连接遇到的问题及调试

    今天在MAC环境下连接mongodb,遇到了一些报错,最终调试全部搞定.在此特做记录! 首先,mongod启动失败 上面有一句话是 exception in initAndListen: 20 Att ...

  10. 3611: [Heoi2014]大工程

    3611: [Heoi2014]大工程 链接 分析: 树形dp+虚树. 首先建立虚树,在虚树上dp. dp:sum[i]为i的子树中所有询问点之间的和.siz[i]为i的子树中有多少询问点,mn[i] ...