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_思维的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

  4. 【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 ...

  5. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  6. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  7. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  8. 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 ...

  9. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

随机推荐

  1. SQL外键的作用

    貌似很有用,但没有真正用过: SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3) ...

  2. 聊聊Shiro

    Shiro是项目中用的比较多Java安全框架,能满足大多数项目的安全认证.授权流程.相比SpringSecurity的复杂重量级,它更简单易用. Shiro中最关键的两个概念是认证和授权,前者解决确认 ...

  3. 洛谷P2943 清理——DP

    题目:https://www.luogu.org/problemnew/show/P2943 一眼看去就有个 n^2 的做法:f[i] = min{ f[j] + num( i - j ) * num ...

  4. appcompat_v7 引起的新建Android工程编译不过的问题 (转载)

    转自:http://blog.csdn.net/zhao7134/article/details/34449641 eclipse adt 上新建基于Android4.4(kk)的Android项目时 ...

  5. POJ2800:Joseph's Problem(等差数列)

    传送门 题意 计算 \(\sum_{i=1}^n(kmodi)\) 分析 1.n>k 直接输出k*(n-k) 2.n<=k 我们发现k/i相同的k%i构成一个等差数列,那么我们从k/i-& ...

  6. SAS基础 -- 逻辑库不存在问题解决

    SAS安装 -- 逻辑库不存在问题解决 1.SAS打不开,将系统时间改为系统提示错误的时间: 2.请在以下两个路径下,新建SASCFG文件夹(注意红色部分为你电脑SAS的安装路径)   D:\Prog ...

  7. shiro 登录

    @Controllerpublic class LoginController { @RequestMapping(value="/login") public @Response ...

  8. java基础类型数据与String类包装类之间的转换与理解

    数据类型转换一般分为三种: 在java中整型,实型,字符型视为简单数据类型,这些数据类型由低到高分别为:(byte,short,char--int-long-float-double) 简单数据类型之 ...

  9. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  10. 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon

    简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单 扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并.(切法就是每出现一条与y轴平行的线段都切一刀) 对于每一个切出来的矩 ...