leetcode_238. Product of Array Except Self_思维
https://leetcode.com/problems/product-of-array-except-self/
给一个vector<int> nums,输出一个vector<int> res,res[i]为nums中除去nums[i]以外所有数的乘积。且不能使用除法运算,时间复杂度为O(n),空间复杂度尽量小。
思路:首先从左往右遍历,对任意i,可以求得nums[0,i-1]的乘积;再从右往左遍历,对任意i,可以求得nums[i+1,n-1]的乘积。根据题意,只需要额外需要O(1)的空间复杂度。
class Solution
{
public:
vector<int> productExceptSelf(vector<int>& nums)
{
int len = nums.size();
vector<int> res = nums;
res[]=;
cout<<res[]<<endl;
for(int i=; i<len; i++)
res[i] = res[i-] * nums[i-];
int tmp = ;
for(int i=len-; i>=; i--)
{
res[i] = res[i]*tmp;
tmp *= nums[i];
}
return res;
}
};
leetcode_238. 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 ...
随机推荐
- js控制页面显示
两个菜单切换显示页面内容: js控制代码, /** JS初始化 **/ $(document).ready(function() { $('#email_btn').click(function(){ ...
- 编译Android VNC Server【转】
本文转载自:http://www.cnblogs.com/fengfeng/p/3289292.html 1,在如下地址checkout源代码,我checkout的版本为0.9.7http://cod ...
- 基于 IOCP 的通用异步 Windows Socket TCP 高性能服务端组件的设计与实现
设计概述 服务端通信组件的设计是一项非常严谨的工作,其中性能.伸缩性和稳定性是必须考虑的硬性质量指标,若要把组件设计为通用组件提供给多种已知或未知的上层应用使用,则设计的难度更会大大增加,通用性.可用 ...
- js调用的注意项
注意将js代码写在调用的前面 这样他就知道了 自己所调用的函数是什么了
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
在SQL server 中,有两种日期时间类型: 1.DateTime 它的范围是 1753-1-1 至 9999-12-31 2.DateTime2 它的满园是 0001-01-01 至 9999- ...
- 关于loadrunner运行场景时提示“初始化失败,通信错误”的解决方案
1)在loadrunner的安装目录下的bin文件夹下有个“wlrun.exe”的文件 2)右键点击“属性”->"兼容性"->兼容模式中选择“windows xp(se ...
- bzoj 4820: [Sdoi2017]硬币游戏【kmp+高斯消元】
有点神,按照1444的做法肯定会挂 注意到它的概率是相同的,所以可以简化状态 详见http://www.cnblogs.com/candy99/p/6701221.html https://www.c ...
- bzoj 2337 [HNOI2011]XOR和路径【高斯消元+dp】
首先,我们发现,因为是无向图,所以相连的点之间是有"依赖性"的,所以不能直接用dp求解. 因为是xor,所以按位处理,于是列线性方程组,设$ x[i] $为点i到n异或和为1的期望 ...
- P3161 [CQOI2012]模拟工厂
传送门 先枚举选择哪些订单,然后转为判定是否可行 在能完成的情况下肯定是花越多时间提高生产力越优 我们设可以有\(x\)单位时间来提高生产力,那么如果当前离下一个订单的时间为\(T\)时,这个订单要\ ...
- C plus plus sprintf用法
sprintf int sprintf ( char * str, const char * format, ... ); Write formatted data to string Compose ...