leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客
原文地址
题目地址
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.)
Subscribe to see which companies asked this question
解法(java):
public int[] productExceptSelf(int[] nums) {
// if (nums == null || nums.length < 2) {
// return null;
// }
int first = nums[0];
int product = 1;
int numZero = 0;
for (int val : nums) {
if (val == 0) {
numZero++;
if (numZero == 2) {
break;
}
} else {
product *= val;
}
}
nums[0] = (numZero == 2) ? 0 : (numZero == 0 ? product / first : (first == 0 ? product : 0));
for (int i = 1; i < nums.length; i++) {
nums[i] = (numZero == 2) ? 0 : (numZero == 0 ? product / nums[i] : (nums[i] == 0 ? product : 0));
}
return nums;
}
leetcode:238. Product of Array Except Self(Java)解答的更多相关文章
- 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 ...
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- Java [Leetcode 238]Product of Array Except Self
题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- [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 ...
- LeetCode 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 ...
- (medium)LeetCode 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 ...
- C#解leetcode 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 ...
- [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 ...
- leetcode 238 Product of Array Except Self
这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...
- Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法
1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...
随机推荐
- python-水仙花数
>>> for a in range(1,10):... for b in range(0,10):... for c in range(0,10):... x=100*a+10*b ...
- pytorch系列 -- 9 pytorch nn.init 中实现的初始化函数 uniform, normal, const, Xavier, He initialization
本文内容:1. Xavier 初始化2. nn.init 中各种初始化函数3. He 初始化 torch.init https://pytorch.org/docs/stable/nn.html#to ...
- Hibernate-04
HQL查询语法 查询:public class Dome{ Session session = HibernaeUitls.openSession(); Transaction tx = sessio ...
- 【笔记】mysql入门语句8条
1.连接到数据库服务器 mysql -h host -uroot -pXXXX 2.查看所有库 show databases; 3.选库 use 库名 4.查看库下面的表 show tables; 5 ...
- 【BZOJ 1084】 [SCOI2005]最大子矩阵(DP)
题链 http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩 ...
- idea 神键
http://blog.csdn.net/dc_726/article/details/42784275 idea 中几个十分酸爽的快捷键.
- 记第一次开发安卓应用——IT之家RSS阅读器
这个学期学校开了安卓的课程,因为自己一直学习wp的开发,一直用的是.net和Silverlight这一套,也着实没有太多时间投入安卓的方向去,因为想着毕业也不从事安卓的工作,所以也一直没有怎么研究.但 ...
- python request包使用指西
request是Python的一个网络模块包,使用它可以快速写一些强大的爬虫脚本
- 图的最小生成树——Kruskal算法
Kruskal算法 图的最小生成树的算法之一,运用并查集思想来求出最小生成树. 基本思路就是把所有边从小到大排序,依次遍历这些边.如果这条边所连接的两个点在一个连通块里,遍历下一条边,如果不在,就把这 ...
- linux shell symbolic link & soft link, symbol link, link
linux shell symbolic link symbolic link https://en.wikipedia.org/wiki/Ln_(Unix) https://stackoverflo ...