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 ...
随机推荐
- poj 2411((多米诺骨牌问题))
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12854 Accepted: 748 ...
- 01_传智播客iOS视频教程_课程介绍与知识点回顾
OC语法中static关键字不能修饰属性,也不能修饰方法,只能修饰方法中的局部变量.static修饰局部变量之后把局部变量变成静态变量.当方法执行完之后这个变量不会被回收.下次再执行这个方法的时候这个 ...
- UVa 12716 && UVaLive 6657 GCD XOR (数论)
题意:给定一个 n ,让你求有多少对整数 (a, b) 1 <= b <= a 且 gcd(a, b) = a ^ b. 析:设 c = a ^ b 那么 c 就是 a 的约数,那么根据异 ...
- win8 使用notepad++写C代码
1. 安装mingw,这里有个不错的教程 http://www.metsky.com/archives/588.html 2. 在notepad++里做设置, 安装nppexec: nppexec-& ...
- bzoj 4552: [Tjoi2016&Heoi2016]排序【二分+线段树】
二分值mid,然后把>=mid的赋值为1,其他赋值为0,每次排序就是算出区间内01的个数,然后分别把0和1放到连续的一段内,这些都可以用线段树来维护 二分的判断条件是操作完之后q位置上是否为1 ...
- js 调试方法两种
JS的错误捕获一般有下面两种方式: 1. 异常捕获常用方法是 try/catch/ throw /finally 2. 全局捕获window.onerror 1. try/catch/throw/fi ...
- python中threading模块中最重要的Tread类
Tread是threading模块中的重要类之一,可以使用它来创造线程.其具体使用方法是创建一个threading.Tread对象,在它的初始化函数中将需要调用的对象作为初始化参数传入. 具体代码如下 ...
- cenos mkdir 无法创建文件夹,即便文件权限为777
SELinux 拒绝了httpd的方式去读写此目录 chcon -R -t httpd_sys_content_rw_t /var/www/html
- 机器学习基础算法__python实现(基于numpy等基础库)
# 博客转自https://blog.csdn.net/weixin_39561100/article/details/80879211 主要是将<机器学习实战>中的算法实现一遍,后续会慢 ...
- Hdu 5358 First One (尺取法+枚举)
题目链接: Hdu 5358 First One 题目描述: 数组a有n个元素,S[i,j]定义为a[i]+a[i+1]+.....+a[j],问:这个死东西等于多少? 解题思路: 二分肯定超,这个题 ...