题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积。

思路:时间O(n),额外空间O(0)。

  第一次扫一遍,处理nums[0~i-1]的积作为ans[i],这样的ans[i]就得到了i之前所有数之积,那么只剩下i后面所有数之积。

  第二次从后往前扫,跟第一次的做法一样,但是这次得开个临时变量存储积了。

 class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans(nums.size(),);
if(nums.size()==) return ans;
for(int i=; i<nums.size(); i++) ans[i]=ans[i-]*nums[i-]; //i之前的部分先乘 int sum=;
for(int i=nums.size()-; i>=; i--) //i后面部分用个变量sum存。
{
sum*=nums[i+];
ans[i]*=sum;
}
return ans;
}
};

AC代码

LeetCode Product of Array Except Self (除自身外序列之积)的更多相关文章

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

  2. LeetCode——Product of Array Except Self

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

  3. 238. [LeetCode] 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 ...

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

  5. LeetCode: Product of Array Except Self

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

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

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

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

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

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

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

随机推荐

  1. Spark Streaming揭秘 Day24 Transformation和action图解

    Spark Streaming揭秘 Day24 Transformation和action图解 今天我们进入SparkStreaming的数据处理,谈一下两个重要的操作Transfromation和a ...

  2. python中文件的复制

    python中文件的复制 python的os模块有很多文件目录相关的函数,但没有提供直接复制文件的函数,当然可以通过边都边写的方式复制文件.想要直接复制文件可以通过shutil模块 shutil模块是 ...

  3. epoll_create, epoll_ctl和epoll_wait

    参考代码 #include <iostream> #include <sys/socket.h> #include <sys/epoll.h> #include & ...

  4. C#写的SQL聚合函数

    SQL Server 字符串连接聚合函数. 注册程序集: 拷贝“SqlStrConcate.dll”至<sql安装根目录>/MSSQL.1/MSSQL/Binn目录下,执行下面的SQL: ...

  5. Python读写文件 - 转

    http://blog.csdn.net/adupt/article/details/4435615 http://docs.python.org/release/2.5.2/lib/bltin-fi ...

  6. 1201: [HNOI2005]数三角形 - BZOJ

    Description Input 大三角形的所有短边可以看成由(n+1)*n/2个单位三角形的边界组成.如下图的灰色三角形所示.其中第1排有1个灰色三角形,第2排有2个灰色三角形,……,第n排有n个 ...

  7. PAT-乙级-1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  8. POJ 2275 Flipping Pancake

    点我看题目 题意 : 按我自己的理解就是,给你n个数,按照从上到下排列,现在让你进行一系列的操作,让你将数按照从大到小排好. 思路 : 比赛的时候以为要用记录路径的搜索,当时没什么把握,所以没做,今天 ...

  9. POJ2031Building a Space Station

    http://poj.org/problem?id=2031 题意:你是空间站的一员,太空里有很多球形且体积不一的“小房间”,房间可能相距不近,也可能是接触或者甚至是重叠关系,所有的房间都必须相连,这 ...

  10. hdu 3389 Game 博弈论

    思路: 其本质为阶梯博弈; 阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈...     每一步则是将一个集体上的若干个点( >=1 )移到前面去,最后没有点可以移 ...