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之外的全 ...
随机推荐
- IE和主流浏览器
1.添加事件 addEventListener 主流 attachEvent IE 2.移除事件 removeEventListener detachEvent 3.获取事件对象 event ...
- JavaWeb基础:Servlet Response
ServletResponse简介 ServletResponse代表浏览器输出,它提供所有HttpResponse的设置接口,可以设置HttpResponse的响应状态,响应头和响应内容. 生命周期 ...
- alpha发布(技术随笔)
昨天是班级里面每个小组要进行alpha演示,大家都很努力的去做自己的项目.我们nice!组没有演示自己的项目,只一点很惭愧,身为组员没有协助组长按时完成项目,这一点自己也感觉很抱歉,虽然每天感觉自己都 ...
- jQuery 2.0.3 源码分析 bind/live/delegate/on
传统的时间处理: 给某一个元素绑定一个点击事件,传入一个回调句柄处理 element.addEventListener('click',doSomething,false); 这样的绑定如果页面上面有 ...
- 第七章 企业项目开发--本地缓存guava cache
1.在实际项目开发中,会使用到很多缓存技术,而且数据库的设计一般也会依赖于有缓存的情况下设计. 常用的缓存分两种:本地缓存和分布式缓存. 常用的本地缓存是guava cache,本章主要介绍guava ...
- ext afteredit
ext afteredit详解 grid.on("afteredit",afterEidt,grid); function(obj) { obj.row;;//修改过的行从0开始 ...
- Ubuntu 查看/修改文件编码
使用enca工具可以查看和修改文件编码 1.安装 sudo apt-get install enca 2.使用 查看文件编码 enca –L zh_CN file_name 修改文件编码 enca – ...
- Disaster Recovery, High Availability, and Continuous Availability - What's the Difference?
Disaster Recovery, High Availability, and Continuous Availability - What's the Difference? Posted by ...
- OkHttp使用全解析(转)。
Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...
- 两个activity之间传递数据用startActivityForResult方法。
package com.example.testactivityresquest; import android.app.Activity; import android.content.Intent ...