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. Anaconda下安装 TensorFlow 和 keras 以及连接pycharm

    首先在官网下载Anaconda https://www.anaconda.com/download/ 安装时注意 勾选第一个,增加环境变量 安装好后再windows界面打开Anaconda Promp ...

  2. 云计算之KVM虚拟化实战

    1 基础环境规划 1.1 主机环境规划 系统版本 主机名 IP地址 内存 磁盘 CentOS6.9 kvm-node1 10.0.0.200 2G 20G CentOS6.9 kvm-node2 10 ...

  3. JavaScript---ECMA对象

    1.对象的概念及分类 1.1 ECMAScript中没有类,但定义了“对象”,逻辑上等价于其他程序设计语言中的类. var o = new Object(); 1.2 本地对象(native obje ...

  4. 关于移动端video标签层级问题

    这是在微信中正常页面,就是用了一个原生video标签没做任何处理.然后顶部是固定页面顶端的,这个时候向上滑动页面时,会出现下图现象 这个时候正常人都会想到z-index问题,我也是这样想的,可惜很抱歉 ...

  5. 尺取法 poj 2566

    尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...

  6. CONDENSE命令により、文字列から冗長スペースが削除

    CONDENSE 命令により.文字列から冗長スペースが削除されます. CONDENSE c [NO-GAPS]. この命令により.項目 c に先行空白が含まれる場合は削除され.その他の空白列がある場合 ...

  7. IIS发布错误记录

    1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 BeginRequ ...

  8. 3,版本控制git-多人协作

    如果你想获得一份已经存在了的 Git 仓库的拷贝,比如说,你想为某个开源项目贡献自己的一份力,这时就要用到 `git clone` 命令. 如果你对其它的 VCS 系统(比如说Subversion)很 ...

  9. eclipse包名分层级显示

    eclipse包名像文件列表那样显示设置方法

  10. 局域网&广域网&Internet&计算机通信过程

    1.局域网 覆盖范围小(100m以内),自己花钱购买,自己单位来维护,带宽固定的(10M,100M,1000M) 2.Internet ISP,有自己的机房,对网民提供访问Internet连接 3.广 ...