【LeetCode 238】Product of Array Except Self
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的更多相关文章
- 【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 ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 【leetcode❤python】 189. Rotate Array
#-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
随机推荐
- oracle11g 远程登录数据库
oracle11g 远程登录数据库比以往的任何版本都要简单,什么也不用做 1.OEM登录 在浏览器中直接输入,远程数据库的OEM网址,当然要把localhost或者主机名改成ip地址 https: ...
- php克隆 自动加载
PHP加载 include() 函数 include() 函数可获得指定文件中的所有文本,并把文本拷贝到使用 include 函数的文件中. 例子 1 假设您拥有一个标准的页眉文件,名为 " ...
- nandflash学习1——导致nandflash反转的原因【转】
转自:http://blog.csdn.net/gaosentao/article/details/7711311 Bit Flip/Bit Flipping/Bit-Flip/Bit twiddli ...
- JavaScript —— 对象的取值与赋值
可能是因为用惯了 Java ,对一个对象取值/赋值喜欢用 setXXX() 和 getXXX() . 在 JavaScript 中使用 setValue() 时,遇到了个奇怪的问题,所以查了下 Jav ...
- Jalopy 之 HelloWorld —— Jalopy 在 MyEclipse 下的安装与使用
如果你要问我Jalopy是什么.我只能告诉你“它是一个格式化代码的工具”.因为我也是一个初学者. 如果你也是初次接触,那一起来学习下吧! ·安装 1.首先,下载资源 下载地址:http://sourc ...
- Java 异常 —— java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible
项目中有个 WebService 接口,调试时使用 Main 方法运行,别人的机器上都能运行,就笔者的机器出问题.他们说是RP的问题…… 异常信息: java.io.InvalidClassExcep ...
- [原]zoj3772--【水题】线段树区间查询+矩阵乘法
思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments [做浙大校赛的时候没有看这道题,事后做的.思路不是自己的, ...
- asp存储过程
dim s_ip,MyComm s_ip=request.ServerVariables("REMOTE_ADDR") Set MyComm = Server.CreateObje ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- WP7 MD5加密
WP7不支持MD5加密,在网上找了一个实现MD5加密的算法. //Copyright (c) Microsoft Corporation. All rights reserved. using Sys ...