题目

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. 从I/O事件到阻塞、非阻塞、poll到epoll的理解过程

    I/O事件   I/O事件 非阻塞I/O.在了解非阻塞I/O之前,需要先了解I/O事件 我们知道,内核有缓冲区.假设有两个进程A,B,进程B想读进程A写入的东西(即进程A做写操作,B做读操作).进程A ...

  2. Centos7.2内网环境安装MySQL5.7.24

    1.配置本地yum源 内网环境,首先需要配置本地yum源,以解决MySQL的依赖安装,具体参考该文:点击打开 2.查看服务器环境 uname -a 3.去官网下载MySQL安装包 MySQL官网网址: ...

  3. SyntaxError: Use of const in strict mode.

    具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...

  4. webpack.config.js====CSS相关:postcss-loader加载器,自动添加前缀

    1. 在webpack中加载css需要先安装style-loader 和 css-loader cnpm install --save-dev style-loader css-loader 2. 在 ...

  5. 个人博客 attack.cf

    新开了个emlog搭的博客 地址:attack.cf 主要分享一下网络安全方面的东西和一些精品资源 欢迎来访

  6. SpringBoot 数据库操作 增删改查

    1.pom添加依赖 <!--数据库相关配置--> <dependency> <groupId>org.springframework.boot</groupI ...

  7. Garmin APP开发之布局

    上一章节介绍了garmin app开发的入门,包括garmin-sdk,开发工具的安装部署,文章结尾我们新建了我们的第一个app程序Garmin开发-入门: http://tieba.baidu.co ...

  8. Viewcontroller基类

    #import <UIKit/UIKit.h> #import "YQZMutableArray.h" @interface YQZViewController : U ...

  9. robotframework介绍

    1.测试用例使用文本文件(TXT或者TSV文件)保存,使用制表符分隔数据.可以方便的使用任何文本编辑器,或者EXCEL编辑测试用例.也可以使用HTML格式创建用例.2.测试用例中支持变量使用,可以使用 ...

  10. Python3获取大量电影信息:调用API

    实验室这段时间要采集电影的信息,给出了一个很大的数据集,数据集包含了4000多个电影名,需要我写一个爬虫来爬取电影名对应的电影信息. 其实在实际运作中,根本就不需要爬虫,只需要一点简单的Python基 ...