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 ...
随机推荐
- springmvc的异常统一处理
在项目实际开发中,异常的统一处理是一个常态.假如不使用异常统一处理,我们往往需要在service层中捕获异常,并且根据不同的异常在result中的设置不同的code并给予相应的提示.这样可能会导致不同 ...
- JPEG库在Linux系统下的编译和移植【转】
本文转载自: 这篇文章介绍了jpeg库在Linux系统下的编译和移植,经过了亲自的验证,编译首先需要准备以下资源:jpegsrc.v6b.tar.gz(jpeg库),libtool-1.5.26.ta ...
- Java对象与对象引用变量的理解
Java对象及对象引用 首先定义一个简单的类: class User{ int userId; String userName; } 我们在创建对象时,通常会写: User user = new Us ...
- Hibernate关联映射(多对一 --- many-to-one)
转自:https://blog.csdn.net/fengxuezhiye/article/details/7369786?utm_source=blogxgwz9 Hibernate的关联映射关系有 ...
- XDCTF2015代码审计全解
此次CTF WEB2是一个大题,一共4个flag,分别代表:获取源码.拿下前台管理.拿下后台.getshell. 目标站:http://xdsec-cms-12023458.xdctf.win/ 根据 ...
- PCB Genesis SET拼板(圆形板拼板) 实现效果(二)
越来发现Genesis采用Surface多边形数据结构的重要性了,当撑握了多边形缩放,交集, 差集,并集等算法, 想实现PCB拼板简直轻而易举了;当然借助多边形算法可以开发出更多的PCB实用的工具出来 ...
- 前端_vue-cli+element-ui+AceEditor+codemirror+electron-vue
因项目工作需要,目前在研究前端的一些知识.主要想实现一个类似于webstorm,可以实现对本地文件进行增删改查等操作的IDE.下面通过几个专题,循序渐进,对某一些部分进行总结,希望能对你有帮助.(网上 ...
- 【洛谷4769】[NOI2018] 冒泡排序(动态规划_组合数学)
题目: 洛谷 4769 博客页面左下角的嘴嘴瓜封神之战中的题目 分析: 一个排列交换次数为 \(\frac{1}{2}\sum_{i=1}^{n}|i-p_i|\) 的充要条件是这个排列不存在长度为 ...
- Styles and Themens(3)android所有主题表
The Android platform provides a large collection of styles and themes that you can use in your appli ...
- glassfish应用服务器安装配置
1.Glassfish4.0下载地址:https://glassfish.java.net/download.html#gfoseTab 2.将下载的glassfish-4.0.zip传输到服务器/h ...