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 ...
随机推荐
- [Js]缓冲运动
一.运动框架 1.在开始运动时,关闭已有定时器(否则会不断有新的定时器执行) 2.把运动和停止隔开(if/else) 二.缓冲运动 逐渐变慢,最后停止(距离越远速度越大) 速度=(目标值-当前值)/缩 ...
- php解密java的DES加密
echo openssl_decrypt( $密文 ,"des-ecb" , $密钥 );
- java程序(一)----HashMap同时获取键值
快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...
- Java 时间、日期类
1. System类 currentTimeMillis():返回当前时间的long型值.此long值是从1970年1月1日0点0分00秒开始到当前的毫秒数. 此方法常用来计算时间差. 2. Date ...
- UITableViewCell 单元格样式
UITableViewCell 单元格样式作用 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefau ...
- java获取页面编码
文章出自:http://babyjoycry.javaeye.com/blog/587527 在此感谢原作者...\(^o^)/~ 最近研究抓取网页内容,发现要获取页面的编码格式,Java没有现成 ...
- UVA 10970-Big Chocolate
题目: 给你一块M*N的巧克力,问把它切成最小单元需要最少切几刀,分开的就不能一起切了. 分析: 每次切割只能多产生一个部分,分成M*N个部分,必然要切M*N-1刀. 一个长为m宽为n的长方形和m*n ...
- Mysql5.0以上 手工注入
Mysql5.0以上 order by 23 http://www..com/productdet.php?&id=89 and 1=2 UNION SELECT 1,2,3,4,5,6,7, ...
- (转)JS加载顺序
原文:http://blog.csdn.net/dannywj1371/article/details/7048076 JS加载顺序 做一名合格的前端开发工程师(12篇)——第一篇 Javascrip ...
- Eclipse卡死问题解决办法
偶尔浏览到几个eclipse卡死的文章,收集一下. 1. eclipse 3.6卡死 eclipse自动提示反应慢,或者卡死, 有人说这是eclipse 3.6的版本bug, 但是3.5版本好像也有 ...