Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)


注释:用两个指针,前面的指针fromBegin比返回的res少乘一个当前的i,后面的指针也是如此。当i到达结尾时,前后都覆盖到了。


#include <cstdio>
#include <vector>
#include<iostream>
using namespace std; class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
int fromBegin=;
int fromLast=;
vector<int> res(n,); for(int i=;i<n;i++){
res[i]*=fromBegin;
fromBegin*=nums[i];
res[n--i]*=fromLast;
fromLast*=nums[n--i];
}
return res;
}
};
int main(){
vector<int> temp;
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
Solution test;
test.productExceptSelf(temp);
return ;
}

最新文章

  1. ABP源码分析二十三:Authorization
  2. FineUI(专业版)v3.1发布(ASP.NET控件库)!
  3. 获取Mac地址
  4. Sql Server 查看表修改记录
  5. Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目
  6. 使用MySQL Workbench导出MySQL数据库关系图
  7. CoreGraphics QuartzCore CGContextTranslateCTM 用法
  8. C# 委托和Lambda---基础
  9. SharePoint 2013中规划企业搜索体系结构
  10. Apache Commons CLI 开发命令行工具示例
  11. merge into的用法
  12. 用户编辑新建_AngularJS实现
  13. HTTP与HttpServlet
  14. ios 图片转视频
  15. css3 过渡记
  16. Quartz2D 图像处理
  17. 【KMP】Cyclic Nacklace
  18. MVC之国际化
  19. ImageLoader的Jar包加载图片
  20. IO流(3)—字节流

热门文章

  1. [Zedboard Linux系统移植]-从MACHINE_START開始
  2. C#实体更新指定的字段
  3. DBA手记(学习)-library cache pin
  4. 浅析中国剩余定理(从CRT到EXCRT))
  5. python使用tablib库生成xls表格
  6. windows安装Oracle数据库
  7. webpack4+Vue搭建自己的Vue-cli
  8. python的___setattr__魔方方法
  9. django的Cookie-9
  10. C语言堆排序