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. rp2833 网卡以及串口与接插件位置关系

    P13     eth0  (电口--兼容光口,如果使用光口,请将去掉U31以及P13) p12     eth1 P9     /dev/ttyS3 调试口 P10-1  /dev/ttyS2  r ...

  2. 人脸识别技术大总结1——Face Detection & Alignment

    搞了一年人脸识别,寻思着记录点什么,于是想写这么个系列,介绍人脸识别的四大块:Face detection, alignment, verification and identification(re ...

  3. 02 Architecture Overview

    本章提要---------------------------------------------arthiecture, and some componentconnect to oracle这一章 ...

  4. 7 款超炫的 jQuery 插件

    jQuery大大简化了我们的前端代码,因为jQuery的简单和开源,也涌现出了层出不穷的jQuery插件,这些实用的jQuery插件也不断推动着jQuery开源社区的发展.下面精选了几款让人跃跃欲试的 ...

  5. selenium测试(Java)--截图(十九)

    package com.test.screenshot; import java.io.File; import java.io.IOException; import org.apache.comm ...

  6. 使用ffmpeg实现合并多个音频为一个音频的方法

    使用ffmpeg实现合并多个音频为一个音频的方法可以使用ffmpeg的filter功能来进行这个操作,而且效果很好amerge也可以实 使用ffmpeg实现合并多个音频为一个音频的方法 可以使用ffm ...

  7. myslq的索引类型为MyISAM和BDB的表:复合索引下的自增长

    本文源自:http://www.himigame.com/mysql/781.html 3.6.9. 使用AUTO_INCREMENT 可以通过AUTO_INCREMENT属性为新的行产生唯一的标识: ...

  8. php将汉字转换为拼音和得到词语首字母(四)

    <?php function getfirstchar($s0){ $firstchar_ord=ord(strtoupper($s0{0})); if (($firstchar_ord> ...

  9. opengl的矩阵理解

    原文链接:http://blog.csdn.net/byhuang/article/details/1476199 矩阵真的是一个很神奇的数学工具, 虽然单纯从数学上看, 它并没有什么特别的意义, 但 ...

  10. sql 一些题目

    这道SQL笔试题你会怎么写(转) 最近面试了一些Senior BI的候选人,行业经验三年到七年不等,起初觉得这个Level的无需准备笔试题,碍于领导执念,就在真实项目中提取5道SQL题目,这里仅单说其 ...