【51】构建乘积数组
来源:互联网 发布:淘宝推广方式 编辑:程序博客网 时间:2024/06/05 01:15
【51】构建乘积数组
- 时间限制:1秒
- 空间限制:32768K
- 本题知识点: 数组
题目描述
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],
其中B中的元素B[i]=A[0]* A[1]* …* A[i-1]* A[i+1]* …*A[n-1]。
不能使用除法。
牛客网题目链接:点击这里
VS2010代码:
#include<vector>#include<iostream>using namespace std;class Solution {public: vector<int> multiply(const vector<int>& A) { //错误输入情况 vector<int> result; if(A.empty()) return result; //用数组暂存左侧 自上向下结果 result.resize(A.size()); //定义和A等长数组 result[0]=1; for(int i=1; i<A.size(); i++) //0~n-1 { result[i]=result[i-1]*A[i-1]; } int temp=1;//记录右侧累乘结果。 自下向上 for(int i=A.size()-2; i>=0; i--) { temp*=A[i+1]; result[i]=result[i]*temp; } return result; }};////B[0] = 1 * A[1]*A[2]*A[3]***A[N-1]//B[1] = A[0]* 1 *A[2]*A[3]***A[N-1]//B[2] = A[0]*A[1]* 1 *A[3]***A[n-1]//. .//. .//. . //B[N-1]=A[0]*A[1]*A[2]***A[n-2]* 1
说明:将问题分步骤进行
牛客网通过图片:
0 0
- 【51】构建乘积数组
- 51、构建乘积数组
- 数组------构建乘积数组
- 数组:构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 52 - 构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 牛客网 | 构建乘积数组
- 构建乘积数组
- 【模模糊糊】构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 构建乘积数组
- 数据结构 并查集 HDU 1213
- POJ 1797 Heavy Transportation 【最大生成树,Prime】
- IQKeyboardManager 详解
- 基于分布特征的神经网络手写字母识别
- 366.Fibonacci-斐波纳契数列(入门题)
- 【51】构建乘积数组
- Android开发——AsyncTask的使用以及源码解析
- 模拟栈问题(思路)
- IOS masonry动画
- Activiti 查看流程历史记录
- 【HDU 2181】哈密顿绕行世界问题(DFS)
- 完全背包问题----思想的理解
- Spring容器中Bean的作用域之singleton和prototype
- 数据结构 并查集 POJ 2236