[LeetCode238]Product of Array Except Self
题目:
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
代码:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> fromBegin(n);
vector<int> fromLast(n);
fromBegin[] = fromLast[] = ;
for(int i = ; i < n; ++i)
{
fromBegin[i] = fromBegin[i-] * nums[i-];
fromLast[i] = fromLast[i-] *nums[n-i];
}
vector<int> res(n);
for(int i = ; i < n; ++i)
{
res[i] = fromBegin[i]*fromLast[n-i-];
}
return res;
}
};
[LeetCode238]Product of Array Except Self的更多相关文章
- Leetcode238. Product of Array Except Self除自身以外数组的乘积
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- [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 > ...
随机推荐
- TestApe - Unit testing for embedded software
TestApe - Unit testing for embedded software About this site Welcome - This site is TestApe.com. Mos ...
- redis优化配置和redis.conf说明
1. redis.conf 配置參数: #是否作为守护进程执行 daemonize yes #如以后台进程执行,则需指定一个pid,默觉得/var/run/redis.pid pidfile redi ...
- jquery在元素上收购事件
jquery的给元素绑定的事件能够用data方法取出来 通过$(element).data("events")来获取 // 比方给一个button绑定两个click事件 $(&qu ...
- 你真的了解mysql的varchar字段的长度有多少吗?
今天在设计系统字段的时候, 发现自己对varchar还不够了解.我设了一个字段.类型为VARCHER,然后我就往里面测试性了写了东西.发现没有多少就满了.我觉得奇怪,5.5版本以上的MYSQL不是有6 ...
- 2014年度辛星解读css第四节
接下来的这一节我计划解说的是超链接和列表的样式,然后我们做出一个导航栏出来,事实上导航栏是很常见的,可是我们这里做得这个有点并不那么完好,等我们学完了css之后再完好它. ************** ...
- 备份恢复与CRM集成的sharepoint站点
在部署CRM与Sharepoint2010集成文档管理之后,一直担心如果需要在新服务器上重新部署CRM, 那么之前与CRM集成的Sharepoint2010文档内容,是否可以重新正确映射到相应的文档位 ...
- JAVA取整以及四舍五入
JAVA取整以及四舍五入 import java.math.BigDecimal; //引入这个包 public class Test { public static void main(String ...
- php中遍历数组的方法
参考网址:http://www.jb51.net/article/29949.htm 这三种方法中效率最高的是使用foreach语句遍历数组.从PHP4开始就引入了foreach结构,是PHP中专门为 ...
- winfrom 倒计时控件
最近在做一个快递柜项目,要求在用户没有操作的时间到了一分钟,自动返回主页,我于是封装了一个倒计时控件,废话少说,直接上代码 public partial class RemainingTimeUC : ...
- 【OC加强】NSDate的使用方法——日期时间在实际开发中比較有用
(1)日期的最主要知识点就是日期转换成字符串格式化输出,相反就是依照某个格式把字符串转换成日期. (2)一般关于时区的设置非常少用到,仅仅要了解就可以. #import <Foundation/ ...