转载请注明出处:z_zhaojun的博客

原文地址

题目地址

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

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) Subscribe to see which companies asked this question

解法(java):

public int[] productExceptSelf(int[] nums) {
// if (nums == null || nums.length < 2) {
// return null;
// }
int first = nums[0];
int product = 1;
int numZero = 0;
for (int val : nums) {
if (val == 0) {
numZero++;
if (numZero == 2) {
break;
}
} else {
product *= val;
}
}
nums[0] = (numZero == 2) ? 0 : (numZero == 0 ? product / first : (first == 0 ? product : 0));
for (int i = 1; i < nums.length; i++) {
nums[i] = (numZero == 2) ? 0 : (numZero == 0 ? product / nums[i] : (nums[i] == 0 ? product : 0));
}
return nums;
}

leetcode:238. Product of Array Except Self(Java)解答的更多相关文章

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

  2. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

  3. Java [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]  ...

  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 of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  6. (medium)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 ...

  7. C#解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 ...

  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. leetcode 238 Product of Array Except Self

    这题看似简单,不过两个要求很有意思: 1.不准用除法:最开始我想到的做法是全部乘起来,一项项除,可是中间要是有个0,这做法死得很惨. 2.空间复杂度O(1):题目说明了返回的那个数组不算进复杂度分析里 ...

  10. Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法

    1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...

随机推荐

  1. xshell全局设置配色方案

    新建XTerm1.xcs文件,将以下内容黏贴进去,保存退出 [XTerm] text=839496 cyan(bold)=93a1a1 text(bold)=408080 magenta=dd3682 ...

  2. 169. Majority Element@python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. RN传参的问题

    RN父组件通过props属性给子组件传参,假设参数 target={target} 子组件在render函数里 let { target } = this.props; 如果子组件有个 FlatLis ...

  4. Python面向对象之模块和包

    模块 模块的概念 模块是Python程序架构的一个核心概念 所有以.py结尾的源文件都是一个模块: 模块名也是标识符,需要遵循标识符的命名规则: 在模块中定义的全局变量,类,函数,都是直接给外界使用的 ...

  5. python-列表数据类型内置方法

    1 列表数据类型(必考) 1.1 用途:兴趣爱好,多个女朋友 1.2 定义方式:[]内用逗号隔开多个元素,多个元素可以是任意数据类型 fangping_boy_friend_list=['ruixin ...

  6. Python 文件操作(一)

    一.注意事项 A.能调用方法的一定是对象 B.文件的操作流程: 1. 打开文件,得到文件句柄并赋值给一个变量 2. 通过句柄对文件进行操作 3. 关闭文件 二.操作实现方法 '''文件名:小双双文件内 ...

  7. CentOS 7 设置开机自启动

    创建脚本:    #!/bin/bash    echo "hello!" # 启动虚拟环境    cd /data/env/crmenv/bin/    source activ ...

  8. linux 定时任务(注意事项)

    1.在要执行的脚本中,执行其他脚本时,需要加入其他脚本需要的环境变量. 2.路径要写全,绝对路径.命令要写全,使用绝对路径的方式.

  9. PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception

    转https://stackoverflow.com/questions/29117679/spring-transactional-management-propagation-required-i ...

  10. 2017acm南宁现场赛 J题 Rearrangement

    题意: 给定一个2 * n的矩阵, 和 2 * n 个数, 问能不能通过重排列, 使得任意相邻两数不能被3整除 分析: 这题一直卡到最后, 赛后经对面大佬提醒后, 发现统计所有数模三的结果(0,1,2 ...