Description:

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

public class Solution {
public int[] productExceptSelf(int[] nums) { int[] res = new int[nums.length];
res[nums.length - 1] = 1;
for(int i=res.length-2; i>=0; i--) {
res[i] = nums[i+1] * res[i+1];
} int l = 1;
for(int i=0; i<nums.length; i++) {
res[i] *= l;
l *= nums[i];
} return res;
}
}

LeetCode——Product of Array Except Self的更多相关文章

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

  3. LeetCode -- Product of Array Except Self My Submissions Question

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

  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 OJ 238. Product of Array Except Self 解题报告

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

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

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

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

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

随机推荐

  1. Jackson2.1.4 序列化格式化时间

    public class User { private int id; private Date birthday; private double money; private String name ...

  2. datagrid中reoload提交时如何批量提交表单中的查询条件

    看标题描述有点复杂,看下图: 直接将手工添加的一个个字段直接用一句代码完成. $('#dg_sub').datagrid("reload",$('#searchForm').ser ...

  3. sql server拼接一列字段

    有一表,名曰IPSlot,欲取IP整列字段. sql语句,利用sql server的xml auto将表数据转换成xml=> select name= STUFF( REPLACE( REPLA ...

  4. chrome 如何利用快捷键将光标移动到地址栏

    Windows: Ctrl + L 或 Alt + D 或 F6 Mac: Command + LLinux: Ctrl + L

  5. 关于MyEclipse项目的名字的修改对项目导入导出的影响

    不要修改项目名字,不管是在MyEclipse中(.project文件里面的额name会变)还是在G:\MyEclipseData目录下(.project文件里面的额name不会变),否则导入的时候不能 ...

  6. Spring IO platform 简介

    前提:熟悉Spring基础知识. 简介:Spring IO Platform将 the core Spring APIs 集成到一个Platform中.它提供了Spring portfolio中的大量 ...

  7. IOC关注服务(或应用程序部件)是如何定义的以及他们应该如何定位他们依赖的其它服务

    IOC关注服务(或应用程序部件)是如何定义的以及他们应该如何定位他们依赖的其它服务.通常,通过一个容器或定位框架来获得定义和定位的分离,容器或定位框架负责: 保存可用服务的集合 提供一种方式将各种部件 ...

  8. e673. Getting Amount of Free Accelerated Image Memory

    Images in accelerated memory are much faster to draw on the screen. However, accelerated memory is t ...

  9. UVA 1371 - Period(DP)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=4117&mo ...

  10. 远程连接mysql数据库提示:ERROR 1130的解决办法

    From: http://blog.sina.com.cn/s/blog_716844910100welz.html 在linux下使用mysql客户端连接远程mysql服务器报错: [root@Se ...