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 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]
.
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道对撞指针,但是以为要每次都要重新乘。两边开始,对应2个循环。
[一句话思路]:
用res[i]来累乘,降低时间复杂度
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
用res[i]来累乘 指定数组中的每一个数,降低时间复杂度n
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
for (int i = 1; i < n; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int[] productExceptSelf(int[] nums) {
//ini: n, res
int n = nums.length, right = 1;
int[] res = new int[n];
res[0] = 1; //left to right
for (int i = 1; i < n; i++) {
res[i] = res[i - 1] * nums[i - 1];
} //right to left
for (int j = n - 1; j >= 0; j--) {
res[j] *= right;
right *= nums[j];
} return res;
}
}
238. Product of Array Except Self 由非己元素形成的数组的更多相关文章
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- 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 ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- 【刷题-LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...
- [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 ...
- (Skill)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 ...
随机推荐
- 使用wlan接收器经常重新登录怎么办
wlan接收器是一个大功率的网卡,能够接受耿远距离的无线网络,在农村和乡镇很普及,很多家庭里都是用这个装置来接受远距离的CMCC信号.但是在使用的时候会经常出现一些问题,例如我们登陆以后,还没等上网就 ...
- MATLAB的一些应用--最近用的比较多
MATLAB的一些应用--最近用的比较多 1.MATLAB分析信号的频谱 快速Fourier变换(FFT)是离散傅里叶变换的快速算法,他是根据离散傅里叶变换的奇.偶.虚.实等特性,对离散傅里叶变换的算 ...
- 如何理解Robot Framework
不知不觉公众号已经有很多人关注了,小编先谢谢大家的支持-今天我们就来谈一谈我对RF的理解,因为前段时间换了工作,目前RF知识基本用不上了.其实我在上一份工作中大部分都是在反复的使用前面讲过的知识——其 ...
- 批量插入数据利器之SqlBulkCopy
工作中要频繁的处理一些数据导入,又不想手工去做,因此用了神器SqlBulkCopy.在MSDN查看了此类的帮助文档几经波折终于搞定,记录下来方便以后查阅. MSDN实例: using System.D ...
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...
- python 的os的总结
转:http://www.cnblogs.com/BeginMan/p/3327291.html
- SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
SharePoint创建列表并使用程序管理列表 列表是SharePoint开发者输入数据的方式之中的一个.使用Web界面创建一个列表并加入一些数据.过程例如以下: 1. 打开站点. 2 ...
- 10 结构体和类 - —— 《Swift3.0 从入门到出家》
Swift中的面向对象5个要素:枚举.结构体.类.协议.扩展 面向对象研究的是对象,完成一件事情需要多个对象参与,是生活的映射 Swift中结构体和类非常相似,也就是结构体能完成类的所有功能.结构体是 ...
- php与JAVA的RSA加密互通
Java 版本RSA 进行加密解密 在网上查询了好几天,最终找到解决方案,网络上都是通过Cipher.getInstance("RSA"); 而改成Cipher.getInstan ...
- EM64T和64位是不是一个概念啊?他们有什么区别啊,怎么区分啊?
首先我们要解决什么是64位这个问题.究竟什么是64位处理器呢?64 bit是相对于32 Bit而言的,这个位数指的是CPU GPRs(General-Purpose Registers,通用寄存器)数 ...