[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 WITHOUT divide operation.
For A = [1, 2, 3], return [6, 3, 2].
LeetCode上的原题,请参见我之前的博客Product of Array Except Self。
解法一:
class Solution {
public:
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
vector<long long> productExcludeItself(vector<int> &nums) {
int n = nums.size();
vector<long long> fwd(n, ), bwd(n, ), res(n);
for (int i = ; i < n - ; ++i) {
fwd[i + ] = fwd[i] * nums[i];
}
for (int i = n - ; i > ; --i) {
bwd[i - ] = bwd[i] * nums[i];
}
for (int i = ; i < n; ++i) {
res[i] = fwd[i] * bwd[i];
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
vector<long long> productExcludeItself(vector<int> &nums) {
long long n = nums.size(), right = ;
vector<long long> res(n, );
for (int i = ; i < n; ++i) {
res[i] = res[i - ] * nums[i - ];
}
for (int i = n - ; i >= ; --i) {
res[i] *= right;
right *= nums[i];
}
return res;
}
};
[LintCode] Product of Array Except Self 除本身之外的数组之积的更多相关文章
- [LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- Lintcode: Product of Array Exclude Itself
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B wi ...
- [leetcode]238. Product of Array Except Self除了自身以外的数组元素乘积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode Product of Array Except Self (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- Java [Leetcode 238]Product of Array Except Self
题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- leetcode日记 Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- 【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 ...
随机推荐
- SAP 出库单新版
*&---------------------------------------------------------------------* *& Report ZSDR045 ...
- iOS 内存管理
一 . 内存管理 包括内存分配 和 内存清除 1.内存管理的范围 :人和继承于NSObject类的对象都需要进行内存管理,任何非对象类型的对象(基本数据类型 如 int char float doub ...
- swift错误 Expressions are not allowed at the top level
``` ... earlier we said top-level code isn't allowed in most of your app's source files. The excepti ...
- List提取相同元素
List<int> currentList = Cls_Data.SoruceDataIntses[key]; preList = currentList.Intersect(preLis ...
- LInux Shell 快捷键
CTRL 键相关的快捷键: Ctrl + a - Jump to the start of the lineCtrl + b - Move back a charCtrl + c - Terminat ...
- DBNEWNAME工具介绍
下面修改数据库的SID和db_name [root@oracle ~]# su - ora11g db11@oracle /home/ora11g$ db11@oracle /home/ora11g ...
- mac版Camtasia 2.10破解
Camtasia是非常好用的一款录屏.视频编辑.制作的软件.但是这么一款优秀的软件只有30天的试用期,试用期过后便不能使用. 目前网上的破解办法几乎都属于同一种办法: http://www.orsoo ...
- 使用Packet Sniffer抓包和分析(z-stack协议)
以下内容仅是自己学习总结,可能会有错误,有发现问题的欢迎指正(图片可以自己放大,还是比较清晰的). 1.协调器上电,其他设备均不上电,抓包如下: 通过观察可以发现,协调器建立网络成功后,会以15秒为周 ...
- 《DSP using MATLAB》示例Example5.23
代码: conv_time = zeros(1,150); fft_time = zeros(1, 150); % % Nmax = 2048; for L = 1:150 tc = 0; tf = ...
- 给IDEA设置单独的JDK
一.系统参数设置: 1.64位IDEA:增加IDEA_JDK_64系统变量 2.32位IDEA:增加IDEA_JKD系统变量 如图: 二.参考说明 https://intellij-support.j ...