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

题意:

  给定一个数组,对于数组中每一个元素i,求ret[i] = nums[0]*nums[1]...*nums[i-1]*nums[i+1]*...*nums[len-1]。不能用除法,要求时间复杂度O(n).

思路:

  先由左到右的顺序求每个元素左边乘积(ret[i] = nums[0]*nums[1]...*nums[i-1]),在其结果上由右到左求每个元素右边乘积(ret[i] = ret[i]*nums[i+1]*...*nums[len-1])即可,其中nums[0]*nums[1]...*nums[i-1]以及nums[i+1]*...*nums[len-1]都可由常量连乘得到。

C++:

 class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) { int len = nums.size(), temp;
vector<int> ret(len, ); temp = ;
for(int i = ; i < len; i++)
{
temp *= nums[i - ];
ret[i] *= temp;
} temp = ;
for(int i = len - ; i >= ; i--)
{
temp *= nums[i + ];
ret[i] *= temp;
} return ret;
}
};

【LeetCode 238】Product of Array Except Self的更多相关文章

  1. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  2. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  3. 【leetcode❤python】 189. Rotate Array

    #-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...

  4. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  5. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  6. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  7. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  8. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  9. 【LeetCode题解】350_两个数组的交集Ⅱ

    目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...

随机推荐

  1. Photoshop支持ico输出

    1.Photoshop支持ico格式插件下载:ICOFormat.8bi 2.把文件放到ps安装目录:xxx/Plug-ins/File Formats 下 3.重启下ps

  2. JMS基本概念

    原文:http://blog.csdn.net/jiuqiyuliang/article/details/46701559 The Java Message Service (JMS) API is ...

  3. Java:网络编程之UDP的使用

    java.net  类 DatagramSocket 此类表示用来发送和接收数据报包的套接字,数据报套接字是包投递服务的发送或接收点. java.net  类 DatagramPacket 此类表示数 ...

  4. Java:静态导入

    静态导入 importStatic 当类重名时,需要指定具体的包名. 当方法重名时,需要指定具体的类或对象名. 举例如下: import java.util.*; import static java ...

  5. 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统

    开篇:从50开始系统已经由MVC4+EF5+UNITY2.X+Quartz 2.0+easyui 1.3.4无缝接入 MVC5+EF6+Unity4.x+Quartz 2.3 +easyui 1.4. ...

  6. JSP的执行过程及其异常处理机制

    1.JSP的执行过程     虽然JSP感觉上很像一般的HTML网页,但事实上它是以Servlet的形式被运行的.因为JSP文件在第一次运行的时候会先解释成Servlet源文件,然后编译成Servle ...

  7. 大规模视觉识别挑战赛ILSVRC2015各团队结果和方法 Large Scale Visual Recognition Challenge 2015

    Large Scale Visual Recognition Challenge 2015 (ILSVRC2015) Legend: Yellow background = winner in thi ...

  8. 15_采用Pull解析器解析和生成XML内容

    java还提供SAX和DOM用于解析XML Android还集成了Pull解析器——推荐 package cn.itcast.service; import java.io.InputStream; ...

  9. C语言输出当前日期和时间

    #include <stdio.h> #include <time.h> char* asctime2(const struct tm *timeptr) { static c ...

  10. FTP命令详解

    FTP的命令行格式为:ftp -v -d -i -n -g [主机IP或者主机名],其中 -v显示远程服务器的所有响应信息: -n限制ftp的自动登录,即不使用: .n etrc文件: -d使用调试方 ...