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 ...
随机推荐
- 【Java】 剑指offer(17) 在O(1)时间删除链表结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除 ...
- 无状态shiro认证组件(禁用默认session)
准备内容 简单的shiro无状态认证 无状态认证拦截器 import com.hjzgg.stateless.shiroSimpleWeb.Constants; import com.hjzgg.st ...
- [HihoCoder1393]网络流三·二分图多重匹配
题目大意: 班级有$N$名学生,运动会有$M$项不同的比赛,第$i$项比赛每个班需要派出$m_i$名选手参加,编号为i的学生最多同时参加给定的$b_i$项比赛中的任意$a_i$项比赛.根据统计的结果, ...
- 20172302 《Java软件结构与数据结构》实验一:线性结构实验报告
课程:<Java软件结构与数据结构> 班级: 1723 姓名: 侯泽洋 学号:20172302 实验教师:王志强老师 实验日期:2018年9月26日 必修/选修: 必修 实验内容 (1)链 ...
- js获取判断苹果手机机型
原先获取不了苹果系列的型号,但转换思路,先判断是否是苹果,再用分辨率获取型号 //获取手机型号函数beginfunction getPhoneType(){ //正则,忽略大小写var pattern ...
- Saltstack 分发
把master上的hosts文件分发到所有主机 [root@node1 ~]# salt-cp '*' /etc/hosts /etc
- webstorm激活方法
安装完成后,打开 WebStorm, 在打开的 License Activation 窗口中选择第三个选项: License server. 在输入框输入网址即可 最新网址: https://s.tu ...
- [Visual Studio] 自定义类模板
1.找到vs2015/vs2012/vs2017的安装目录下:Common7\IDE\ItemTemplates\CSharp\Code\2052\Class 2.打开Class.cs文件 using ...
- 树莓派.Qt.Creator安装方法
树莓派硬件: Raspberry Pi 3 B 树莓派系统: Linux version 4.9.59-v7+ (32位) Qt版本(x86版本--32位): 安装过程 可以查看软件仓库支持的版本: ...
- Voltage Level-Shifter Output Waveform
http://www.cypress.com/knowledge-base-article/interfacing-sram-jtag-signals-using-voltage-level-shif ...