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的更多相关文章

  1. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  2. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  3. 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 ...

  4. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

  5. 【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 ...

  6. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  7. 【刷题-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 ...

  8. [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 ...

  9. (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 ...

随机推荐

  1. Eclipse汉化问题解决

    1.删除eclipse/configuration 目录下的 org.eclipse.osgi 和org.eclipse.update 两个子目录2.重新启动 eclipse

  2. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...

  3. Windows的同步I/O和异步I/O

    同步I/O操作 执行步骤 1.        程序通过FileStream对象来打开磁盘文件,然后调用Read方法(内部调用Win32 ReadFile函数),从文件中读取数据.这时,线程从托管代码转 ...

  4. 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别

    本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...

  5. 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout

    贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...

  6. 无刷新 checkbox列表的删除

    前台 JS : function ModelDelete() { var checkvalues = null; var checValue = $("#dom1").find(& ...

  7. Chapter 3: Connector(连接器)

    一.概述 Tomcat或者称之为Catalina(开发名称),可以简化为两个主要的模块,如下图: 多个Connector关联一个Container.之所以需要多个Connector,是为了处理多种协议 ...

  8. CGI标准简介 ~ Django

    CGI CGI(Common Gateway Interface)是WWW技术中最重要的技术之一 , 有着不可替代的重要地位 , CGI是外部应用程序(CGI程序)于Web服务器之间的接口标准 , 实 ...

  9. Ubuntu 重新设置网络

    sudo service network-manager stop sudo rm /var/lib/NetworkManager/NetworkManager.state sudo service ...

  10. 删除目录下所有gif的图片

    find  -name "*.gif" -exec rm -fv {} \;