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

题意:给一个序列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 (除自身外序列之积)的更多相关文章
- [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
Description: Given an array of n integers where n > 1, nums, return an array output such that out ...
- 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 ...
- 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 ...
- LeetCode: Product of Array Except Self
Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- 【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 ...
- 【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 ...
随机推荐
- [转]Unity3D游戏开发之数据持久化PlayerPrefs的使用
转自 http://blog.csdn.net/qinyuanpei/article/details/24195977 首先我们来看两段Unity3D中实现数据读写的简单代码吧: //保存数据 Pla ...
- 1009. Product of Polynomials (25)
#include <stdio.h> struct MyStruct { int exp; double coe; }; int main() { int k1,k2,i,j; MyStr ...
- 回顾Ado.Net
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- python学习笔记11(函数二): 参数的传递、变量的作用域
一.函数形参和实参的区别 形参全称是形式参数,在用def关键字定义函数时函数名后面括号里的变量称作为形式参数. 实参全称为实际参数,在调用函数时提供的值或者变量称作为实际参数. >>> ...
- 滤镜简单demo(转,供参考)
NSURL *iamgeUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"default" ...
- [转]用Python做一个自动生成读表代码的小脚本
写在开始(本片文章不是写给小白的,至少你应该知道一些常识!) 大家在Unity开发中,肯定会把一些数据放到配置文件中,尤其是大一点的项目,每次开发一个新功能的时候,都要重复的写那些读表代码.非常烦.来 ...
- springMVC数据封装成POJO
springMVC把前台的数据封装为POJO与struts2的封装形式不同.struts2需要在控制器声明需封装的POJO,而springMVC不需要任何准备工作,只需在相应的方法的参数中加上需封装的 ...
- BZOJ 4123 [Baltic2015] Hacker 解题报告
首先,Alice 会选择一个长度为 $\lfloor\frac{n+1}{2}\rfloor$ 的区间,我们把这个长度记为 $len$. 有这么一个结论:令 $F_i$ 为覆盖 $i$ 点的所有长度为 ...
- eclipse中切换jre后报错:Java compiler level does not match the version of the installed Java project facet.
项目移除原来的jre环境lib后,添加本地的jre,报错如下: Java compiler level does not match the version of the installed Java ...
- [itint5]两有序数组的中位数
这个题和leetcode的基本一样.用了更好点的思路.在A中折半猜是不是中位数,A中没有后在B中猜.最后猜到B[j]<=A[i]<=B[j+1],此时,无论奇偶(2k+1或者2k个),A[ ...