[NeetCode 150] Products of Array Discluding Self

Products of Array Discluding Self

Given an integer array nums, return an array output where output[i] is the product of all the elements of nums except nums[i].

Each product is guaranteed to fit in a 32-bit integer.

Follow-up: Could you solve it in O ( n ) O(n) O(n) time without using the division operation?

Example 1:

Input: nums = [1,2,4,6]

Output: [48,24,12,8]

Example 2:

Input: nums = [-1,0,1,2,3]

Output: [0,-6,0,0,0]

Constraints:

2 <= nums.length <= 1000
-20 <= nums[i] <= 20

Solution

A classic problem. There are 3 kinds of cases.

First, there is no zero in the array. All we need to do is use multiplication law, multiplying all numbers together and the product divided by number at the index is the corresponding answer of that index.

Second, there is only one zero in the array. It is obvious that the answer array will all be zero except where the only zero locates.

Finally, if there are more than one zero in the array, the answer will be a pure-zero array.

Code

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        if nums.count(0) > 1:
            return [0] * len(nums)
        if nums.count(0) == 1:
            answer = [0] * len(nums)
            product = 1
            zero_index = 0
            for i, num in enumerate(nums):
                if num == 0:
                    zero_index = i
                    continue
                product *= num
            answer[zero_index] = product
            return answer
        product = 1
        for num in nums:
            product *= num
        answer = []
        for num in nums:
            answer.append(product//num)
        return answer
            

Remember to use // for the division, otherwise the result will be a float rather than integer.

相关推荐

  1. [NeetCode 150] Valid Sudoku

    2024-07-12 22:58:04       21 阅读
  2. [NeetCode 150] Word Ladder

    2024-07-12 22:58:04       23 阅读
  3. [NeetCode 150] Redundant Connection

    2024-07-12 22:58:04       26 阅读
  4. [NeetCode 150] Longest Consecutive Sequence

    2024-07-12 22:58:04       21 阅读
  5. [NeetCode 150] Products of Array Discluding Self

    2024-07-12 22:58:04       24 阅读
  6. [NeetCode 150] Merge K Sorted Linked Lists

    2024-07-12 22:58:04       26 阅读
  7. LeetCode 150, 112, 130

    2024-07-12 22:58:04       19 阅读
  8. DAY 10 | 1047, (20,150)

    2024-07-12 22:58:04       52 阅读
  9. 面试经典150题(96-100)

    2024-07-12 22:58:04       54 阅读
  10. 面试经典150题(108-110)

    2024-07-12 22:58:04       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 22:58:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 22:58:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 22:58:04       58 阅读
  4. Python语言-面向对象

    2024-07-12 22:58:04       69 阅读

热门阅读

  1. NCNN源码学习(1):Mat详解

    2024-07-12 22:58:04       19 阅读
  2. Spring Boot对接大模型:实战价值与技巧

    2024-07-12 22:58:04       20 阅读
  3. 算法学习记录3

    2024-07-12 22:58:04       21 阅读
  4. linux的CUDA、torch和驱动GPU驱动的对应问题

    2024-07-12 22:58:04       19 阅读
  5. 递归函数遍历格式化字典

    2024-07-12 22:58:04       22 阅读
  6. 【LeetCode】2089. 找出数组排序后的目标下标

    2024-07-12 22:58:04       22 阅读
  7. 简谈设计模式之单例模式

    2024-07-12 22:58:04       20 阅读
  8. Linux文件系统

    2024-07-12 22:58:04       18 阅读
  9. 进程的阻塞

    2024-07-12 22:58:04       24 阅读
  10. 连接docker私有仓库

    2024-07-12 22:58:04       22 阅读