LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/
Submissions: 87262 Difficulty: Medium
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.)
Subscribe to see which companies asked this question
给出一个数组,要求计算一个新数组。数组里全部的元素都是除了自己以外的元素乘积。而且要求不许用除法。
《编程之美》上的一道原题。创建两个辅助数组。一个保存全部左边元素乘积的结果。一个保存全部右边元素乘积的结果。借助这两个数组,一次遍历就能够得到结果。
我的AC代码
public class ProductofArrayExceptSelf {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4 };
System.out.print(Arrays.toString((productExceptSelf(a))));
}
public static int[] productExceptSelf(int[] nums) {
int len = nums.length;
int[] r = new int[len];
int[] left = new int[len];
int[] right = new int[len];
left[0] = nums[0];
for (int i = 1; i < len; i++) {
left[i] = left[i - 1] * nums[i];
}
right[len - 1] = nums[len - 1];
for (int i = len - 2; i >= 0; i--) {
right[i] = right[i + 1] * nums[i];
}
r[0] = right[1];
r[len - 1] = left[len - 2];
for (int i = 1; i < len - 1; i++) {
r[i] = left[i - 1] * right[i + 1];
}
return r;
}
}
LeetCode OJ 238. Product of Array Except Self 解题报告的更多相关文章
- 【LeetCode】238. Product of Array Except Self 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 两次遍历 日期 题目地址:https://leetcode.c ...
- 【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 OJ: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 ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- 【九度OJ】题目1052:找x 解题报告
[九度OJ]题目1052:找x 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1052 题目描述: 输入一个数n ...
随机推荐
- BZOJ1264 [AHOI2006]基因匹配Match 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1264 题意概括 给出两个长度为5*n的序列,每个序列中,有1~n各5个. 求其最长公共子序列长度. ...
- [OpenCV-Python] OpenCV 中的图像处理 部分 IV (六)
部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 23 图像变换 23.1 傅里叶变换目标本小节我们将要学习: • 使用 OpenCV 对图像进行傅里叶变换 • ...
- codeforces 366C Dima and Salad 【限制性01背包】
<题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...
- 洛谷 p1123 取数游戏【dfs】
题目链接:https://www.luogu.org/problemnew/show/P1123 转载于:>>>>>> 题目描述 一个N×M的由非负整数构成的数字矩 ...
- python套接字编程基础
python套接字编程 目录 socket是什么 套接字的工作流程 基于tcp的套接字 基于udp的套接字 socket是什么 客户端/服务器架构(C/S架构) 服务端:提供服务的一端 客户端:请求服 ...
- Java中的访问权限控制
Java提供了public, private, protected 三个访问权限修饰词,提供了以下四种访问权限控制机制: 1.包访问权限: 2.Public访问权限: 3.Private访问权限: 4 ...
- 线性代数与Python
1.向量1.1向量例子1.2向量加法与减法1.3向量的乘法2.矩阵2.1矩阵例子2.2矩阵的形状2.3矩阵的创建函数 1.向量 向量是指可以加总(以生成新的向量),可以乘以标量(即数字),也可以生成新 ...
- Oracle 删除重复数据只留一条(转)
转自:http://www.cnblogs.com/252e/archive/2012/09/13/2682817.html 查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是 ...
- UITableView滚动优化(RunLoop)
链接: 利用RunLoop优化tableView RunLoop方式优化加载tableview RunLoop总结:RunLoop的应用场景(三)滚动视图流畅性优化 TableView加载图片的优化逻 ...
- 碰到在Windows中访问局域网文件夹, 提示无法访问时的解决办法
运行:gpedit.msc 找到下图的位置, 启用即可