原题

思路:

注意时间复杂度,分别乘积左右两边,可达到O(n)

class Solution {
public:
vector<int> productExceptSelf(vector<int> &nums) {
int len = nums.size();
vector<int> res(len, 1);
int left = 1, right = 1;
for (int j = 1; j < len; j++) {
left *= nums[j - 1];
res[j] *= left;
} for (int i = len - 2; i >= 0; i--) {
right *= nums[i + 1];
res[i] *= right;
}
return res;
}
};

[leetcode] 238. Product of Array Except Self (medium)的更多相关文章

  1. LN : leetcode 238 Product of Array Except Self

    lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...

  2. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

  3. (medium)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 ...

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

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

  6. Java [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]  ...

  7. C#解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 ...

  8. [leetcode]238. 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 ...

  9. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

随机推荐

  1. How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)

    The Dynamic Link Library (DLL) is stored separately from the target application and shared among dif ...

  2. qt截获html请求(继承QNetworkAccessManager和QNetworkReply)

    QtWebkit加载html页面,html中会有很多的请求,比如<img id="testImg" src="http://*.jpg" width=&q ...

  3. Qt的槽可以使用默认参数

    引用自:http://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/signal-slot/ 的一篇经典文章,是关于Qt的信号和槽的分析的.看年份 ...

  4. MongoDB自学日记2——权限

    首先应该明确的是为什么要学MongoDB.OK,如果是仅仅出于对于流行技术的原始兴趣,可能并不能深入学习,还必须有应用需求.刚开始学习MongoDB,因为以前对其它数据库的了解也不是特别深入,所以许多 ...

  5. spring之@Value详解(转载)

    @Value注入 不通过配置文件的注入属性的情况 通过@Value将外部的值动态注入到Bean中,使用的情况有: 注入普通字符串 注入操作系统属性 注入表达式结果 注入其他Bean属性:注入beanI ...

  6. 从此Redis是路人

    从此Redis是路人 序言:Redis(Remote DIctionary Server)作为一个开源/C实现/高性能/基于内存的key-value存储系统,相信做Java的小伙伴都不会陌生.Redi ...

  7. 解决Nextcloud 无法删除目录

    1)进入维护模式 sudo -u www php /www/wwwroot/192.168.40.159/occ maintenance:mode --on 2)使用mysql命令行工具,在nextc ...

  8. Android开发之旅(1) 之 Android 开发环境搭建

    工作室原创出品,欢迎转载,欢迎交流. 转载请注明原文:http://www.cnblogs.com/wangleiblog/p/6019063.html Android开发之旅目录 1 前言 很多朋友 ...

  9. springboot 2.X 在访问静态资源的的时候出现404的问题

    通过idea快速搭建一个springboot项目: springboot版本2.1.6 在网上看的资料,springboot静态资源访问如下: "classpath:/META‐INF/re ...

  10. 一文看懂Python的面向对象编程

    之前在网络上看了很多关于面向对象的编程详解,还是不够过瘾,所以决定自己动手写一篇. 面向对象:Object Oriented Programming,简称OOP,即面向对象程序设计. 类(Class) ...