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之外的全 ...
随机推荐
- 定位 absolute和relative比较
absolute:脱离原来位置定位.是相对于最近的有定位的父级进行定位;如果没有有定位的父级元素,就相对文档进行定位 relative:保留原来位置进行定位,相对于自己原来的位置进行定位 下面举两个例 ...
- 杭电 2141 Can you find it? (二分法)
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- luogu3380 【模板】二逼平衡树(树套树)
#include <iostream> #include <cstdlib> #include <cstdio> #include <ctime> us ...
- cf842d Vitya and Strange Lesson
#include <iostream> #include <cstdio> using namespace std; int s[2000005][2], cnt, n, m, ...
- Ubuntu 16.04安装JDK7/JDK8的两种方式
ubuntu 安装jdk 的两种方式:1:通过ppa(源) 方式安装. 2:通过官网下载安装包安装. 这里推荐第1种,因为可以通过 apt-get upgrade 方式方便获得jdk的升级 使用ppa ...
- Linux基础之重定向|grep
重定向 > :覆盖输出>> :追加输出 2> :重定向错误输出2>> : 追加方式 &> : 重定向标准输出或者错误输出 ...
- hihoCoder#1139 二分·二分答案
原题地址 挺简单一道题,结果因为一时傻逼写错一个小地方,导致WA成狗了_(:з」∠)_ 代码: #include <iostream> #include <cstring> # ...
- bzoj3875 【Ahoi2014】骑士游戏 spfa处理后效性动规
骑士游戏 [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,JYY一共有两种攻 ...
- java Web项目Service层通用接口和entityVo对象与entity对象转化问题的解决方案
Service层的接口中有一些比较常用方法,一次又一次的在新的Service层中被书写,所以懒惰的程序员又烦了,他们决定写个通用接口来解决这个问题. 有些项目中,实体类即承担接收表单数据的任务,又承担 ...