Product of Array Except Self - LeetCode
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.)
思路:构建结果数组res,然后从头到尾扫描一遍nums数组。
res[i]所存的值为nums[0]到nums[i - 1]的乘积,res[0] = 1。
最后从尾到头再扫描一遍nums数组,res[i]这次再乘上nums[i + 1]到nums[n - 1]的值。
所乘上的值可以只由一个变量来计算完成。这里用一个int变量,命名为right,初始为1。
则res[i]乘以right就是最后结果值。要注意的是,每次乘完之后,right要更新为right * nums[i]。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums){
int n = nums.size();
vector<int> res(n, );
for (int i = ; i < n; i++)
res[i] = (i == ? : res[i - ] * nums[i - ]);
int right = ;
for (int i = n - ; i >= ; i--)
{
res[i] *= right;
right *= nums[i];
}
return res;
}
};
Product of Array Except Self - LeetCode的更多相关文章
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- 【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 ...
- 【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: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 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【刷题-LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
- [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 ...
随机推荐
- CSS需要注意的问题1(转生活因拼搏而精彩的网易博客)
1.检查HTML元素(如:<ul>.<div>).属性(如:class=”")是否有拼写错误.是否忘记结束标记(如:<br />) 因为Xhtml 语 ...
- laravel5.2总结--blade模板
## 1.基本用法 ``` ##情形1 $name = laravel5 <div class="title"> {{$name}} {{$name}}</div ...
- Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)
什么是NIC Teaming? NIC Teaming 就是将两个或更多的网络适配器组合在一起,从而达到容错和带宽聚合作用.NIC Teaming 中的每个网络适配器都是物理存在的(虚 ...
- 我给女朋友讲编程CSS系列(2)- CSS语法、3大选择器、选择器优先级
首先看一下使用Css设置h1标签字体颜色和大小的例子,效果图如下: 新建一个网页test.html,然后复制粘贴下面的内容: <html> <head> <style t ...
- 【Linked List Cycle II】cpp
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- Python对文本文件的简单操作(一)
工作背景 性能测试工程师,主要测试工具--loadrunner,主要是接口测试. 实现功能 loadrunner对报文格式的转换存在问题,部分报文无法转换,故使用Python编写脚本自动将soap协议 ...
- git放弃修改&放弃增加文件
1. 本地修改了一堆文件(并没有使用git add到暂存区),想放弃修改. 单个文件/文件夹: git checkout -- filename 所有文件/文件夹: git checkout . 2. ...
- Zookeeper ZooDefs.Ids
OPEN_ACL_UNSAFE : 完全开放的ACL,任何连接的客户端都可以操作该属性znode CREATOR_ALL_ACL : 只有创建者才有ACL权限 READ_ACL_UNSAFE:只能读 ...
- Solr 配置连接数据库
前面我们将solr安装并创建了core同时也配置可IK分词器,接下来我们通过配置连接Mysql数据库并把数据导入到solr(使用ik分词器). 1.配置managed-schema文件 Request ...
- 【转】Unity3D研究院之两种方式播放游戏视频
http://www.xuanyusong.com/archives/1019 Unity3D中播放游戏视频的方式有两种,第一种是在游戏对象中播放,就好比在游戏世界中创建一个Plane面对象,摄像 ...