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. android-exploitme(四):参数篡改

    今天我们来测试请求中参数的篡改,这个在web安全测试中是常用的,拦截请求包,修改参数,提交 1.  首先我们需要启动模拟器,并使用本机的代理(加上参数-partition-size的目的是为了可以往a ...

  2. Centos环境下部署游戏服务器-自动化

    计算机是没有智力的,只会接受指令,运行指令,得出结果.因此就算你让它重复做一件事多少次,它也学不会自动去做这件事.对于重复繁琐的事情,做为一个懒惰的程序员,必须告诉机器去做这件事情,然后就行了,而不是 ...

  3. java string,需要进行首字母大写改写

    java string,需要进行首字母大写改写,网上大家的思路基本一致,就是将首字母截取,转化成大写然后再串上后面的,类似如下代码 //首字母大写     public static String c ...

  4. 【推荐】HTML5 UI框架 推荐

    笔者的男装网店:http://shop101289731.taobao.com .冬装,在寒冷的冬季温暖你.新品上市,环境选购 最近自己瞎搞一下web网页的东西.想开发先找资源. 整理了一下HTML5 ...

  5. Android:在eclipse中快速多行注释的方法

    http://blog.csdn.net/jianghuiquan/article/details/8534337 也许你能够记住以下部分快捷键,对你开发和设计过程中大裨益! 1.//注释添加和取消 ...

  6. JUnit + Spring + Hibernate 集成测试,无法通过的问题

    使用JUnit测试DAO层.由于不能破坏数据现场,故所有的测试类都继承了Spring测试框架下的 org.springframework.test.AbstractTransactionalDataS ...

  7. python3代码

    import urllib.request url="http://mm.taobao.com/json/request_top_list.htm?type=0&page=1&quo ...

  8. 选错实施顾问公司 ERP项目九死一生

    今天接到一个朋友的电话,他是一家企业老总.这位老总感到非常头疼的是他的企业选择了一款国际上名气很大的ERP软件,但实施效果却强差人意.他的疑问是"不是说只要选对了ERP产品,谁实施都能成功吗 ...

  9. leetcode Database2 (四)

    一.Duplicate Emails Write a SQL query to find all duplicate emails in a table named Person. +----+--- ...

  10. java中final关键字

    一.final修饰方法 禁止任何继承类修改它的定义,保证在继承中使方法行为保持不闲并且不会被覆盖. final修饰的方法,同意编译器针对该方法的调用转为内嵌调用.(类似c++ 中的inline?) p ...