LeetCode -- Product of Array Except Self My Submissions Question
Question:
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.)
Analysis:
给出有n个元素的整数数组(n > 1) nums, 返回一个数组输出,其中output[i]为除了nums[i]外其它个元素的乘积。不要分割数组并且在O(n)的时间内解决这个问题。
例如给出数组:[1, 2, 3, 4],返回[24, 12, 8, 6].
注意:
分析:
在计算乘除时,0是一个特殊元素,还要考虑正负号的问题。因此我们的思路是:
a. 一般情况下(无0元素):为避免溢出,用一个long型的参数存储所有元素的乘积,然后循环数组一次,依次除以当前元素的值,将除数保存到output数组中即可;
b. 若数组中含有0元素,但是我们不知道0元素的个数有多少,因此需要用另外一个参数zero对零元素计数。如果数组中仅含一个0,则只有0元素的位置为其他所有元素的乘积,其他的元素都为0;如果数组中含有多余一个0,则所有位置都为0.
本题很简单,只要按照特殊元素0分类即可。
Answer:
public class Solution {
public int[] productExceptSelf(int[] nums) {
long temp = 1;
int zero = 0;
int[] result = new int[nums.length];
for(int x : nums) {
if(x == 0)
zero++;
else temp *= x;
}
if(zero > 1)
return result;
else if(zero == 1){
for(int i=0; i<nums.length; i++) {
if(nums[i] == 0)
result[i] = (int) (temp);
}
return result;
}
else {
for(int i=0; i<nums.length; i++) {
if(nums[i] == 0)
result[i] = (int) (temp / nums[i]);
result[i] = (int) (temp / nums[i]);
}
return result;
}
}
}
LeetCode -- Product of Array Except Self My Submissions Question的更多相关文章
- [LeetCode] 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——Product of Array Except Self
Description: Given an array of n integers where n > 1, nums, return an array output such that out ...
- 238. [LeetCode] 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: Product of Array Except Self
Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...
- LeetCode Product of Array Except Self (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- [LeetCode] 11. Container With Most Water My Submissions Question 解题思路
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- 【LeetCode】Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
随机推荐
- react的 react-router使用
官方API:https://reacttraining.com/react-router/web/api/BrowserRouter; React Router 安装命令如下. 使用时,路由器Rout ...
- 用struct LNode *L与LinkList &L的区别
用void InitList(struct LNode *L), 函数InitList中如果改变了L指针本身的值,对其他函数无影响.用void InitList(LinkList &L),函数 ...
- C/C++获取本机名+本机IP+本机MAC
本机名.IP.MAC都是一些比较常用网络参数,怎么用C/C++获取呢? 研究了两三个小时... 需要说明的都在代码注释里 #include <stdio.h> #include <W ...
- lintcode_111_爬楼梯
爬楼梯 描述 笔记 数据 评测 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的 ...
- docker快速安装jenkins
用过docker的人,可能真的很难忍受再一步步二进制安装了,好了话不多说,感慨一下jenkins实现自动化发布构建真的很方便. 推荐一个学习的好地方https://m.w3cschool.cn/jen ...
- python json.dumps raise TypeError(repr(o) + " is not JSON serializable") TypeError: 0 is not JSON serializable
出错如题. 这个问题有可能是因为python的json.dumps没法识别dump内容里的某些数据类型导致的.我的问题是因为dict中含有numpy.int64,numpy.float等类型导致的,需 ...
- Python 中关于文件操作的注意事项
文件操作 #打开文件 f = open('要打开的文件路径',mode = 'r/w/a', encoding = '文件原来写入时的编码') #操作 data = f.read() #读取 f.wr ...
- mybatis <forEach>标签的使用
MyBatis<forEach>标签的使用 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis.当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名 ...
- 15、python之导入模块
一.什么是模块? 模块本质是一个py文件,我们可以通过关键字import将py文件对象导入到当前名称空间. 二.导入模块 1.import module 2.from module import ob ...
- python基础之装饰器扩展和迭代器
wraps模块 让原函数保留原来的说明信息 import time import random from functools import wraps def auth(func): '''auth ...