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. JSON 与 XML 的比较 - iOS

    在与 web 服务进行数据交换的时候,通常支持两种主要的数据格式(即:JavaScript 对象表示法 JSON 与可扩展标记语言 XML),两者在可读性上都不分高下,接下来对此进行简单的总结和分析, ...

  2. Python 初始—(文件操作)

    文件修改,我们可以不用讲一个文件全部都进行读取,然后放入内存,如果文件过大,容易造成内存的 内存溢出问题 因此我们可以便读取边进行修改操作 f=open("old.txt",&qu ...

  3. FAT32中文版分析+补充(2)

    从Offset 36(0x24)开始FAT12/16的内容开始区别于FAT32,现在分两个表格列出来,下表为FAT12/16的内容: 名称 Offset(Byte) 大小(Byte) 描述 BS_dr ...

  4. SpringMVC使用ModelAndView的相对路径和绝对路径的问题

    例如:abc/a/a.jsp,想要跳转到根目录的b.jsp 使用 ModelAndView 跳转, 若引用:org.springframework.web.portlet.ModelAndView 这 ...

  5. Mybatis基础入门学习

    Mybatis基础入门学习 mybatis架构分析 搭建测试mybatis架构 )下载并导入mybatis3.2.7.jar(架构),mysql-connector-java-5.1.7-bin.ja ...

  6. spring-JDBC Template

    JDBC Template概念 为简化持久化操作,spring在JDBC API之上提供JDBC Template组件 提供统一模板: 环境配置 1.创建MySQL数据库 2.搭建maven项目,并引 ...

  7. django+xadmin在线教育平台(十三)

    这个6-8对应对应6-11,6-12 拷入forgetpassword页面 书写处理忘记密码的view users/views.py # 用户忘记密码的处理view class ForgetPwdVi ...

  8. 手机丢了怎么办?MZ给你来支招

    1致电运营商挂失手机 2致电银行冻结手机网银 3手机绑定支付宝的拨95188挂失 4微信用户登录110.qq.com冻结账号 5修改微博.微信.QQ等密码 6到手机运营商处补手机卡. 一定要记住啊!手 ...

  9. 493. Reverse Pairs

    // see more at https://www.youtube.com/watch?v=j68OXAMlTM4 // https://leetcode.com/problems/reverse- ...

  10. 27-Middleware管道介绍

    1-Middleware管道介绍,. 如果匹配上/task,则界面只会显示i am task. public void Configure(IApplicationBuilder app, IHost ...