题目

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

Solve it without division and in O(n).

For example, given [1,2,3,4], return [24,12,8,6].

分析

给定一个数组序列,得到一个新的数组,新数组的output[i]位置元素值等于原数组中除了nums[i]外所有其他元素的乘积。

并且要求时间复杂度为O(n)

我们最先想到的肯定是二重循环,计算每个位置处的 output[i];但是,这样处理复杂度为O(n^2),不满足要求;

所以采取其它方法,分为两个步骤处理:

  1. 正序循环一次,对每个i位置的元素得到ret[i] = nums[0] * nums[1] * … *nums[i-1]
  2. 倒序循环一次,对每个i位置的元素当前乘积ret[i] *= nums[i+1] … nums[sz-1]

AC代码

class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
if (nums.empty())
return vector<int>(); int sz = nums.size();
vector<int> ret(sz, 0); //对每个i位置的元素得到ret[i] = nums[0] * nums[1] * ... *nums[i-1]
ret[0] = 1;
for (int i = 1; i < sz; ++i)
{
ret[i] = ret[i - 1] * nums[i-1];
}//for //对每个i位置的元素当前乘积ret[i] *= nums[i+1] ... nums[sz-1]
int tmp = 1;
for (int i = sz - 1; i >= 0; --i)
{
ret[i] *= tmp;
tmp *= nums[i];
}
return ret;
}
};

GitHub测试程序源码

LeetCode(238) Product of Array Except Self的更多相关文章

  1. LeetCode(88)Merge Sorted Array

    题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...

  2. LeetCode(40)-Merge Sorted Array

    听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...

  3. LeetCode(108) Convert Sorted Array to Binary Search Tree

    题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. jQuery banner切换插件

    今天学写了一个基于jQuery焦点图切换插件,有不对的地方还请多多指教,不多说下面是代码: 1.引jQuery库 <script src="http://code.jquery.com ...

  2. replcation set (复制集)配置过程 --mongodb

    一,配置规划 复制集原理(基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB)  Paxos(mysql MGR 用的是变种))如果发生主库宕机,复制集内部会进行投票选举,选择一 ...

  3. (转)nginx限制上传大小和超时时间设置说明/php限制上传大小

    nginx限制上传大小和超时时间设置说明/php限制上传大小 原文:http://www.cnblogs.com/kevingrace/p/6093671.html 现象说明:在服务器上部署了一套后台 ...

  4. 关于使用memcached提高并发的文章,很有用

    http://blog.csdn.net/ywh147/article/details/9385137 http://phl.iteye.com/category/292555 memcached 解 ...

  5. css3响应式表格

    <h1>极客学院相关课程</h1> <table class="responsive"> <thead> <tr> &l ...

  6. js之深度克隆、简易克隆

    一.js中的对象 谈到对象的克隆,必定要说一下对象的概念. js中的数据类型分为两大类:原始类型和对象类型. (1)原始类型包括:数值.字符串.布尔值.null.undefined(后两个是特殊的原始 ...

  7. GoAccess安装和使用介绍

    使用文档参考地址:https://my.oschina.net/mrco/blog/181737https://www.fanhaobai.com/2017/06/go-access.html goa ...

  8. 如何选择Web开发框架

    下面先来看看为什么要使用Web开发框架一 使用框架的必然性框架,即framework.其实就是某种应用的半成品,把不同应用程序中有共性的一些东西抽取出来,做成一个半成品程序,这样的半成品就是所谓的程序 ...

  9. RAC数据库后台进程介绍

    在RAC数据库上会比单实例数据库多一些进程,这些进程是RAC特有的,为了实现集群数据库功能而设置的. 10g RAC特有进程:$ ps -ef|grep ora_oracle    4721     ...

  10. 【UML】对象图Object diagram(转)

    http://blog.csdn.net/sds15732622190/article/details/48894751 前言 今天要说的是UML中的对象图.他与类图,合作图都有关系,是类图的实例化. ...