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. Linux ad7606 驱动

    Linux中已经移植好了ad7606,位于driver/staging/iio/adc/目录中.只要在板级文件中添加device中即可. 移植参考文档: https://wiki.analog.com ...

  2. e586. Drawing Simple Shapes

    There are two ways to draw basic shapes like circles, ovals, lines, arcs, squares, rectangles, round ...

  3. 控件activeX开发之项目ffmpeg解码h264——总结

    1. 编译好ffmpeg的lib库和dll库 2. 播放器作为一个dilog类player,然后在ctrol中的oncreate重写方法中用全局属性cplayer *player里new cplaye ...

  4. sdut2852 小鑫去爬山9dp入门)

    #include<stdio.h> int a[100][100]; int main() { int n; while(scanf("%d",&n)!=EOF ...

  5. centos6.5的软件安装,用户管理等

    1.软件的安装命令: 第一种:以 软件名.rpm 为结尾的二进制文件,这样的文件的安装 rpm -ivh 软件名.rpm i 是 install v 是校验 h 进度 卸载软件 是 rpm -e 软件 ...

  6. 【Java面试题】53 能不能自己写个类,也叫java.lang.String?

    可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...

  7. mysql 启动报错--发现系统错误2,系统找不到指定的文件。

    解决方法: 控制面板--找到mysql程序--修复

  8. jquery promise

    认识jQuery的Promise   先前了解了ES6的Promise对象,来看看jQuery中的Promise,也就是jQuery的Deferred对象. 打开浏览器的控制台先. <scrip ...

  9. iperf/netperf网络性能测试工具、Wireshark网络包分析工具

    iperf   http://www.linuxidc.com/Linux/2014-05/101160.htm netperf  http://www.linuxidc.com/Linux/2013 ...

  10. 【HMM】隐马尔科夫模型

    http://www.hankcs.com/nlp/hmm-and-segmentation-tagging-named-entity-recognition.html