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 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]
.
代码如下:
public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] output=new int[nums.length];
List<Integer> list=new ArrayList<>();
int sum=1;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=0)
sum=sum*nums[i];
else list.add(i);
} if(list.size()==1)
{
output[list.get(0)]=sum;
return output;
}
else if(list.size()>1)
return output; for(int i=0;i<nums.length;i++)
output[i]=sum/nums[i]; return output; }
}
238. Product of Array Except Self的更多相关文章
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- 【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] 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 ...
- (Skill)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 ...
随机推荐
- 12个强大的Chrome插件
Chrome功能强大,也得益于其拥有丰富的扩展资源库.Chrome Web Store里有各种各样的插件,可以满足你使用Chrome时的各种要求.和Firefox一样,Chrome的扩展非常容易安装, ...
- TCP/IP 某些最常见的错误原因码 (errno)列表
对于在基于 UNIX 的环境中的 TCP/IP 用户,下表列出了某些最常见的错误原因码 (errno).它不是完整的错误列表.可以在文件 /usr/include/sys/errno.h 中找到 Er ...
- MVC 3个重要的描述对象之ControllerDescriptor
1.ControllerDescriptor 1.1 ReflectedControllerDescriptor public class HomeController : Controller { ...
- EntityFramework之创建数据库及基本操作(一)
那时学EF的时候还没有Code First,只有DB First,生成的是一个EDMX文件,Code First则没有这文件,下面直接上代码吧 数据库创建以及建表 1.首先我们新建一个新项目,使用Nu ...
- mongo .update
db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...
- Chart For Asp.Net ----Overview
一个图表有很多元素构成,所有元素都能通过图表API控制.图表API是面向对象的,可扩展的,高复用的.支持很多图表元素如:data series,data points in a series,char ...
- java基础之 泛型
泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...
- 【转】./configure && make && make install详解
在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装! 我们都知道源 ...
- matlab调用opencv函数的配置
环境: VS2010 活动解决方案平台x64 WIN 8.1 Opencv 2.4.3 Matlab 2012a 1. 首先保证vs2010能正确调用opencv函数, 2. Matlab中选择编 ...
- IOS UITableView的分隔线多出问题
如题,有时显示UITableView多出部分在页面时,下面会显示处多出的行, 此时应该在UITableView初始化时设置为Group if (_tableView == nil) { _tableV ...