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 ;
}

238. [LeetCode] Product of Array Except Self的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode——Product of Array Except Self

    Description: Given an array of n integers where n > 1, nums, return an array output such that out ...

  5. LeetCode -- Product of Array Except Self My Submissions Question

    Question: Given an array of n integers where n > 1, nums, return an array output such that output ...

  6. LeetCode: Product of Array Except Self

    Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...

  7. LeetCode Product of Array Except Self (除自身外序列之积)

    题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...

  8. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  9. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

随机推荐

  1. css让不同大小的图片适应div的大小,且不变形。

    做成背景图片 单个 .imgdiv { width: 100px; // 你要的正方形 height: 100px; // 你要的正方形 background-image: url(/your/ima ...

  2. Oracle分析函数巧妙使用

    在 Oracle中使用Sql必须弄懂分析函数 Oracle开发专题之:分析函数(OVER) 1 Oracle开发专题之:分析函数2(Rank, Dense_rank, row_number) 6 Or ...

  3. JS の 套路 II ~~

    今天的需求是 给表单赋值 还有修改并保存.以下的方法应该是个本方法,但好上手!! 给表单值 <form> <table> <tbodu> 这里假装有一堆表单的一对t ...

  4. ora.ctssd OBSERVER

    [grid@ydb1 ~]$ crsctl status res -t -init    ora.ctssd      1        ONLINE  ONLINE       ydb1       ...

  5. Mysql-数据的完整性约束

    一 .介绍 二 .not null与default 三 .unique 四 .primary key 五 .auto_increment 六 .foreign key 一 .介绍 约束条件与数据类型的 ...

  6. 写 5 个你知道的 HTML5 标签,说明他们的意义

    1:<canvas>标签   定义图形.图像,是基于javascript的绘图API 2:<video>标签   定义视频 3:<audio>标签  定义音频内容 ...

  7. LeetCode 910. Smallest Range II

    很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...

  8. Java程序设计 第16周 课堂实践 —— 数据库3

    Java程序设计 第16周 课堂实践 -- 数据库3 课堂实践任务3 查询world数据库,获得New Jessey州所有城市的总人口数. 代码分析 实现查询数据库需要我们修改MessageDAO.j ...

  9. 20155329实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 1实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 2. 初步掌握单元测试 ...

  10. Ubuntu + apache + Mysql +php

    发生了乱码问题: 打开apache配置文件: sudo gedit /etc/apache2/apache2.conf,在最后面加上:AddDefaultCharset UTF-8,如果还乱码,再将U ...