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之外的全 ...
随机推荐
- jQuery图片延迟加载插件jQuery.lazyload使用方法(转)
使用方法 1.引用jquery和jquery.lazyload.js到你的页面 <script src="jquery-1.11.0.min.js"></scri ...
- robotframework笔记26
测试数据文档工具(Testdoc) Testdoc是机器人框架内置的工具生成高水平 根据测试用例文档. 创建的文档是在HTML中 格式和它包括名称.文档和其他元数据 测试套件和测试用例,以及和他们的顶 ...
- 使用Visual Studio 2013进行单元测试--初级篇
1.打开VS2013 --> 新建一个项目.这里我们默认创建一个控制台项目.取名为UnitTestDemo 2.在解决方案里面新增一个单元测试项目.取名为UnitTestDemoTest 创建完 ...
- 《Java程序设计》第五周学习总结
20145224 <Java程序设计>第五周学习总结 教材学习内容总结 第八章异常处理 8.1.1使用try.catch ·教材范例用户连续输入整数,输入0结束后显示输入数的平均值(代码如 ...
- php 正则匹配中文
在javascript中,要判断字符串是中文是很简单的.比如:var str = "php编程";if (/^[\u4e00-\u9fa5]+$/.test(str)) {aler ...
- ThinkPHP框架如何修改X-Powered-By
以前用ThinkPHP框架开发了一个小网站,前几天查询页面HTTP状态发现,里面有一项: X-Powered-By: ThinkPHP 2.0 这样虽然没什么,但感觉如果别有用心的人查询会知道你是用这 ...
- hdu 3172 Virtual Friends (映射并查集)
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- MFC学习之窗口基础
WinMain函数 1.句柄(HANDLE):{ 1. 定义:资源的标识 2. 句柄的作用: 操作系统通过句柄来找到对应的资源,从而对这些资源进行管理和操作. 3句柄的分类:(按资源){ 1.图标句柄 ...
- ABAP版连连看
网上看到的,感觉不错,借来装13... *&---------------------------------------------------------------------* *&a ...
- 开源牛人 zcbenz
事情是这样的,微软推出了Visual Studio Code,我很好奇他怎么做跨平台的,所以就找找资料,在他的网站中是这么描述的: Architecturally, Visual Studio Cod ...