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 ...
随机推荐
- 类数组对象:arguments
在js中调用一个函数的时候,我们经常会给这个函数传递一些参数,js把传入到这个函数的全部参数存储在一个叫做arguments的东西里面,那它到底是什么呢? 一.描述 arguments 是一个对应于传 ...
- bisect模块用于插入
参考链接: chttp://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html水
- 企业建站系统MiinCMP1.0.5 版公布!
2014-5-4,在青年节,Juuluu公布了其企业建站系统的新版1.0.5,经过两周多的奋战,Juuluu团队为MiinCMP新浪云版的移植工作做了大量工作.1.0.5已可完美执行于国内免费的jav ...
- 6 字典和集合——《Swift3.0从入门到出家》
字典和集合 字典 字典是集合类型存放多个键值对,其中键是唯一的,不能重复 字典中存放的键值对是无序的,写入的顺序和读取的顺序可能不同 字典中存放的数据是任意类型 字典可以分为可变字典和不可变字典 创建 ...
- [Err] 1067 - Invalid default value for 'xxxTime'
下面是导入sql脚本的的局部脚本 `xxxTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 发现是NO_ZERO_IN_DATE,NO_Z ...
- Timer的异常
定时任务用Timer实现有可能出现异常,因为它是基于绝对时间而不是相对时间进行调度的.当环境的系统时间被修改后,原来的定时任务可能就不跑了.另外需要注意一点,捕获并处理定时任务的异常.如果在Timer ...
- jQuery选择器this通过onclick传入方法以及Jquery中的this与$(this)初探,this传处变量等
起初以为this和$(this)就是一模子刻出来.但是我在阅读时,和coding时发现,总不是一回事. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- MySQL error : Deadlock found when trying to get lock; try restarting transaction
在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”. 在 14. ...
- 【转】JMeter工作基本原理
JMeter可以作为Web服务器与浏览器之间的代理网关,以便捕获浏览器的请求和Web服务器的响应,这样就很容易地生成性能测试脚本, 有了性能测试脚本,JMeter就可以通过线程组来模拟真实用户对Web ...
- Vue.js:目标结构
ylbtech-Vue.js:目标结构 1.返回顶部 1. Vue.js 目录结构 上一章节中我们使用了 npm 安装项目,我们在 IDE(Eclipse.Atom等) 中打开该目录,结构如下所示: ...