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.)
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size(),);
int i;
for(i=;i<nums.size();i++)
{
res[i]=res[i-]*nums[i-];
}
int right=;
for(i=nums.size()-;i>=;i--)
{
res[i]*=right;
right*=nums[i];
}
return res;
}
};
Product of Array Except Self的更多相关文章
- [LintCode] Product of Array Except Self 除本身之外的数组之积
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...
- 【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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
随机推荐
- iOS 简单动画 序列帧动画
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"旭宝爱吃 ...
- 巧用Xode中的代码块(转)
在我们编码的过程中,总会发现有一些重复的编码工作,可能你会不厌其烦的去copy and paste,但是你有去考虑过怎样解决这样的问题吗?其实现在很多的编程IDE都能帮助我们减少一些编码的任务,提高编 ...
- 关于JS变量提升的一些坑
function log(str) { // 本篇文章所有的打印都将调用此方法 console.log(str); } 函数声明和变量声明总是会被解释器悄悄地被“提升”到方法体的最顶部 变量声明.命名 ...
- Spring CharacterEncodingFilter
<!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> <filter-name>springUtf8Encoding</fi ...
- groups, usermod, chown, chgrp, chmod
Linux文件权限简介 Linux的每个文件可以由三种用户访问 属主权限:创建人的权限 属组权限:与创建同一个用户组的权限 其他权限:和创建人不在同一个用户组的用户的权限 当然,root用户拥有最高权 ...
- 神奇的expect
想写自动化脚本的时候,遇到需要交互的,如ssh,scp,就束手无策,直到我知道了expect. expect 有一系列expect-send对组成,就像聊天一样. expect A send B ex ...
- C++之STL
5.子类模板访问基类模板在子类模板中访问那些在基类模板中声明且依赖于模板参数的符号,应该在它前面加上作用域限定符"::" 或者显示使用this指针否则,编译器将试图在全局域中寻找该 ...
- ubuntu16.04下vim安装失败
问题? 重装了ubuntu系统,安装vim出现了以下问题: sudo apt-get install vim 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取 ...
- VC6.0 C++ 如何调用微软windows系统SDK 语音API
下载3个语音API安装包 http://www.microsoft.com/en-us/download/details.aspx?id=10121 需要安装微软语音API安装包:SpeechSDK5 ...
- 151008-JS初级完成,PHP入门(变量常量等)-没假放了
hi 今天是10月8号,你懂的,好困好困哒 上午搞定了JS的入门篇,真的是入门篇,基本都是JS做基本的输出.样式变化.惯例给出代码 <!DOCTYPE HTML><html>& ...