leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思:
1、不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨。
2、空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里面
做法:既然不用除法,对于某个数i, result[i] = 0到i - 1的乘积 X i + 1 到 n - 1的乘积
具体来说,先正向遍历,result[i] 存储的是 0 到i - 1的乘积,再反向遍历,乘上另一半,这就同时满足了时间复杂度和空间复杂度的要求
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
if(nums.empty())
return vector<int>();
vector<int> result(nums.size(), 1);
int accums = 1;
for(int i = 0; i < nums.size() ; i++){
result[i] = accums;
accums *= nums[i];
}
accums = 1;
for(int i = nums.size() - 1; i >= 0; i--){
result[i] *= accums;
accums *= nums[i];
}
return result;
}
};
leetcode 238 Product of Array Except Self的更多相关文章
- 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 ...
- 剑指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] * ...
- [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 ...
- 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 ...
- (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 ...
- 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] ...
- 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 ...
- [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 ...
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...
随机推荐
- MySQL DELETE 表别名问题
- Facade 运行机制
举一个常见的例子在routes.php路由文件中的Route就是使用了laravel的Facade; config/app.php 文件中的aliases数组: 再例如其中的mail: 里面只是简单的 ...
- selenium+python笔记5
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 登陆126邮箱 """ f ...
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- android:configChanges属性
对android:configChanges属性,一般认为有以下几点: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏 ...
- javascript function new this
1. 首先,我们这里把function直接调用时将这个function当做方法来看待,而new function是将function当做类来看待 2. 当把function作为类来使用时,functi ...
- 实验一 Java开发环境的熟悉境的熟悉
- 运行结果: (一)命令行下Java程序开发 (二)Eclipse下Java程序开发.调试 设置断点和单步运行 单步运行:Step Into(快捷捷F5)和Step Over(快捷捷F5) 使程序直 ...
- BZOJ1932 [Shoi2007]Setstack 集合堆栈机
妈呀...clj大爷太强啦! 原来还有set_union和set_intersection这种东西... 于是只要把栈顶的每个元素hash一下记录到一个vector里去就好了 /*********** ...
- apache 日志为每个域名独立配置单独的日志文件
<VirtualHost *:80>DocumentRoot "E:\luyou\viplijiang"ServerName vip.li.comTransferLog ...
- Linux 下配置多机实时同步
没钱的时候,用此方案做网站内容的负载均衡.异地备份,经济实惠又方便(仅针对网站文件做实时同步,如果数据库,则考虑mysql的多主架构) 一.机器配置及机房IP A机,位于杭州,IP: 115.33.2 ...