52.Product of Array Except Self(除过自身的数组乘积)
Level:
Medium
题目描述:
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
思路分析:
我们可以求出整个数组的乘积allProduct,然后,result[ i ]就为allProduct除以nums[ i ],如果数组中有零,那么除过为零元素对应位置外,其他位置对应结果都为零。
代码:
public class Solution{
public int []productExceptSelf(int[]nums){
int allProduct=1;
int[] res=new int[nums.length];
int flag=0; //标志出现零
for(int i=0;i<nums.length;i++){
if(nums[i]==0&&flag==0){
flag=1;
continue;
}
allProduct=allProduct*nums[i];
}
for(int i=0;i<res.length;i++){
if(flag==1){
if(nums[i]!=0)
res[i]=0;
else
res[i]=allProduct;
}else{
res[i]=allProduct/nums[i];
}
}
return res;
}
}
52.Product of Array Except Self(除过自身的数组乘积)的更多相关文章
- [LintCode] Product of Array Except Self 除本身之外的数组之积
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...
- [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 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 除本身之外的数组之积
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
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
随机推荐
- 基于mesos 安装 jenkins
mesos master 机子上安装 jenkins git clone https://github.com/jenkinsci/mesos-plugin.git && cd me ...
- UIView与CALayer 区别
在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个 ...
- 源码分析--LinkedList(JDK1.8)
LinkedList与ArrayList一样都是List接口的实现类,底层用双向链表实现. LinkedList本身用一个内部类实现链表元素. private static class Node< ...
- 微信小程序(14)--上传图片公用组件(父子传参)
这周整理了一下做微信小程序页面时遇到的一些问题,先说说常见的上传图片吧. 上传图片公用组件 首先要了解的是父子传参. 1.A组件为父组件,B组件为子组件,以下是A组件向B组件传参: 在A组件的json ...
- 三、MVC_JsonResult类型
一.Ajax或者页面请求获取数据,不通过WebApi的时候,使用JsonResult作为返回Json数据格式的类型 二.代码呈现 public class HomeController : Contr ...
- java插件提示安全设定高,不能加载解决方法
当不可信小应用程序或应用程序在 Web 浏览器中运行时,我应当如何控制? 本文适用于: Java 版本: 7.0, 8.0 通过 Java 控制面板设置安全级别 在 Java 控制面板中,单击 Sec ...
- NodeJs初相识
一.nodeJs简介 1.Node 是一个服务器端 JavaScript 解释器. 2.Node 的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处理数万条同时连接到一个物理机的连接代码.处理高并 ...
- kotlin中实现匿名内部类
1.常规的方式实现匿名内部类 valueAnimator.addUpdateListener(object :AnimatorUpdateListener { override fun onAnima ...
- EXCUTE JAVAScript点击事件
# Execute Javascript document.getElementsByClassName('chooseFile')[${index}].arguments[0].click(); # ...
- 【串线篇】Mybatis之动态sql
一.if标签 <select id="getTeacherByCondition" resultMap="teacherMap"> select * ...