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(除过自身的数组乘积)的更多相关文章

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

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

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

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

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

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

  7. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

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

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

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

随机推荐

  1. 本机ip地址怎么查

     转自:https://www.192ly.com/basic/local-ip-address-lookup-method.html 百度搜索一下[IP],你就可以轻松看到你的IP地址了,百度出来的 ...

  2. 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(二)

    前言 已完成数据预处理工作,具体参照: 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(一) 设置配置文件 新建目录face_faster_rcn ...

  3. C++使用函数strcpy出现bug: 错误 C4996 'strcpy': This function or variable

    C++中使用函数strcpy时出现问题: 解决方案: 在文件开头添加语句: #pragma warning(disable:4996) done! 剑指offer: 第一题:赋值运算符函数 #incl ...

  4. ltp-ddt nand_perf_ubifs_w_cpuload

    NAND_M_PERF_UBIFS_CPU_LOAD source 'common.sh';/opt/ltp/runltp -f ddt/nand_perf_ubifs -s "NAND_S ...

  5. Hibernate 一对一(基于唯一外键的关联)

    主表 hbm.xml中 使用<one-to-one> 从表hbm.xml中使用<many-to-one> 并指定unique=true people.hbm.xml: < ...

  6. ivew 绑定时间控件

    <FormItem label="开始时间" style="width: 100%" prop="startDate"> < ...

  7. vue新建项目之饿了么组件标准配置

    main.js import Vue from 'vue' import App from './App.vue' import ElementUI from 'element-ui'; import ...

  8. JSP页面中<%!%>与<%%>与<%=%>详解

    首先,我们要了解jsp运行原理.JSP的本质就是一个Servlet,JSP的运行之前会先被Tomcat服务器翻译为.java文件,然后在将.java文本编译 为.class文件,而我们在访问jsp时, ...

  9. 51nod 1661: 黑板上的游戏(sg函数 找规律)

    题目链接 先打一个sg函数的表,找找规律,发现sg函数可以递归求解 打表代码如下 #include<bits/stdc++.h> using namespace std; typedef ...

  10. python使用bs4爬取boss静态页面

    思路: 1.将需要查询城市列表,通过城市接口转换成相应的code码 2.遍历城市.职位生成url 3.通过url获取列表页面信息,遍历列表页面信息 4.再根据列表页面信息的job_link获取详情页面 ...