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 ...
随机推荐
- kali视频(21-25)学习
第六周 kali视频(21-25)学习 21.密码攻击之在线攻击工具 22.密码攻击之离线攻击工具(一) 23.密码攻击之离线攻击工具(二) 24.密码攻击之哈希传递攻击 25.无线安全分析工具 21 ...
- ASP.NET的几个试题(《C#与.NET程序员面试宝典》)
更多参考:博客园笔记 :ASP.NET是什么 ASP.NET不是一种语言,而是创建动态Web页的一种强大的服务器端技术,它是Microsoft.NET Framework中一套用于生成Web应用程序和 ...
- 布局类,让多个div在一行显示
原文链接:http://www.divcss5.com/wenji/w472.shtml
- asp select count(*) 用 open还是excute
dSql1="select count(*) from test_hist where uid="&cid 'dRs1.open dSql1,tConn,1,1 'dS ...
- Python学习笔记之os模块
Python中的os提供了非常丰富的方法用来处理文件和目录,下面我们将详细的介绍os相关的一些方法和函数: os 路径相关的函数: 1.os.listdir(dirname):列出dirname目录下 ...
- git fatal: remote origin already exists. 报错解决
在研究git的时候,随便输了个 git remote add origin xxx; 然后再真正add 远程仓库的时候,报了git fatal: remote origin already exist ...
- c#实现QQ群成员列表导出及邮件群发之模拟QQ登陆
主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员
- GitFlow在客户端Sourcetree的使用
安装 Sourcetree中直接集成了gitflow工具,可以在界面上找到 初始化 首次按下Git Flow按钮后,会弹出如图窗口 初始化会规定几个特殊的分支名称 生产环境分支:master 开发 ...
- java 随意控制控件的位置
package chat1; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class chat1{ ...
- (转)oracle的split函数
本文转载自:http://www.cnblogs.com/linbaoji/archive/2009/09/17/1568252.html PL/SQL 中没有split函数,需要自己写. 代码: c ...