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 ...
随机推荐
- WebService_使用三要素
一.Java中WebService规范 JAVA 中共有三种WebService 规范,分别是JAX-WS.JAX-RS.JAXM&SAAJ(废弃). 1.JAX-WS规范 JAX-WS 的全 ...
- 第一个Java程序示例——Hello World!【转】
本文转载自: 跟随世界潮流,第一个Java程序输出“Hell World!”. 通过Eclipse运行程序 启动Eclipse,在菜单中选择“文件 --> 新建 --> Java项目”,弹 ...
- YTU 2864: 分跑道。
2864: 分跑道. 时间限制: 1 Sec 内存限制: 128 MB 提交: 23 解决: 19 题目描述 有N个人参加100米短跑比赛.跑道为8条.程序的任务是按照尽量使每组的人数相差最少的原 ...
- YTU 1010: 目标柏林
1010: 目标柏林 时间限制: 1000 Sec 内存限制: 64 MB 提交: 32 解决: 15 题目描述 1945年初,苏军和英美联军已从东西两面攻入德国国境. 4月初,在苏军和英美联军的 ...
- 日元兑换——国内兑换需要护照和签证,国外的机场有兑换ATM
在中国换日元:在中国的商业银行都可以换取日元,但是换汇者必须持有护照.签证等材料.换汇的汇率是按照即时汇率进行结算,如是现钞则按钞买价兑换,另外还要收取0.5%的手续费. 在日本换日元:除了在日本银行 ...
- xcode 8.1 (8B62)真机调试配置
1.点击菜单栏中的Xcode->Preferences->Accounts,如图: 点击上图左下角中的“+”号,登陆一个Apple id(前提已经有了一个apple id账号), 2.然后 ...
- 单纯形 BZOJ3112: [Zjoi2013]防守战线
题面自己上网查. 学了一下单纯形.当然 证明什么的 显然是没去学.不然估计就要残废了 上学期已经了解了 什么叫标准型. 听起来高大上 其实没什么 就是加入好多松弛变量+各种*(-1),使得最后成为一般 ...
- 05_传智播客iOS视频教程_第一个OC程序
Cocoa Application开发的是带界面的程序. OC是完全兼容C语言的,但是C语言里面是不能写OC的东西的. OC和C的第一个区别,就是源文件的后缀名的区别.OC程序的源文件的后缀名是.m, ...
- asp.net web.config配置节说明(转发)
原文地址:http://www.cnblogs.com/qingyuan/articles/1501644.html web.config 文件查找规则: (1)如果在当前页面所在目录下存在 ...
- bzoj 2194: 快速傅立叶之二【NTT】
看别的blog好像我用了比较麻烦的方法-- (以下的n都--过 \[ c[i]=\sum_{j=i}^{n}a[i]*b[j-i] \] 设j=i+j \[ c[i]=\sum_{j=0}^{n-i} ...