[LeetCode] Multiplication Withtout Division

Question:

There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. Solve it without division operator and in O(n).

For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1].

Example:

A: {4, 3, 2, 1, 2}

OUTPUT: {12, 16, 24, 48, 24}

http://leetcode.com/2010/04/multiplication-of-numbers.html

// Time: O(n)
int[] calc(int[] A)
{
    // Assumptions:
    // A is not null, not empty.
    int len = A.length;
    
    // Define B[i] = A[0] * A[1] * ... * A[i];
    // B[0] = A[0];
    // B[i] = B[i - 1] * A[i];
    //
    // Similarly, define C[i] = A[i] * A[i + 1] * ... * A[len - 1]
    int[] B = new int[len];
    B[0] = A[0];
    
    int[] C = new int[len];
    C[len - 1] = A[len - 1];
    
    for (int i = 0 ; i < len ; i ++)
    {
        if (i > 0)
            B[i] = B[i - 1] * A[i];

        int j = len - 1 - i;
        if (j < len - 1)
            C[j] = C[j + 1] * A[j];
    }
    
    int[] toReturn = new int[len];
    for (int i = 0 ; i < len - 1 ; i ++)
    {
        int a = i == 0 ? 1 : B[i - 1];
        int b = i == len - 1 ? 1 : C[i + 1];
        toReturn[i] = a * b;
    }
    return toReturn;
}

// See the link as a better solution without extra space
int[] calc(int[] A)
{
    int len = A.length;
    
    int left = 1; // A[0] * A[1] * ... A[i - 1]
    int right = 1; // A[i + 1] * ... A[len - 1]
    // O[i] = left * right;
    
    int[] O = new int[len];
    Arrays.fill(O, 1);
    
    for (int i = 0 ; i < len ; i ++)
    {
        O[i] *= left;
        O[len - 1 - i] *= right;
        left *= A[i];
        right *= A[len - 1 - i];
    }
}
时间: 2024-10-14 10:37:07

[LeetCode] Multiplication Withtout Division的相关文章

LeetCode 399. Evaluate Division

原题链接在这里:https://leetcode.com/problems/evaluate-division/description/ 题目: Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the a

[LeetCode] 399. Evaluate Division Java

题目: Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0. Example:Given a /

[leetcode]Graph-399. Evaluate Division

Equations are given in the format A / B = k, where  A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0. Example:Given a / b =

【博弈论】Multiplication Game

题目描述 Alice and Bob are in their class doing drills on multiplication and division. They quickly get bored and instead decide to play a game they invented.The game starts with a target integer N≥2, and an integer M = 1. Alice and Bob take alternate tu

Multiplication Game

题目描述 Alice and Bob are in their class doing drills on multiplication and division. They quickly get bored and instead decide to play a game they invented. The game starts with a target integer N≥2, and an integer M = 1. Alice and Bob take alternate t

Multiplication Game(博弈论)

Description Alice and Bob are in their class doing drills on multiplication and division. They quickly get bored and instead decide to play a game they invented. The game starts with a target integer N≥2N≥2 , and an integer M=1M=1. Alice and Bob take

poj 3134 Power Calculus(迭代加深dfs+强剪枝)

Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x. The operation of squaring can be appreciably shorten the sequence of multiplications.

POJ1365:素数

Prime Land Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3552   Accepted: 1609 Description Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,

Python新手必备练习4---开发一个计算器程序

总是有朋友问我,在听我讲的课时感觉都能听懂,我讲的例子照着写也能做出来,但一到自己想不照抄而是自己写的时候,就发现完全没有思路,不知如何下手.对此我只能说,还是因为练习的少,平常从来不写代码,学了点语法就想啪啪啪实现复杂的功能是不现实的,学习语言是一个循序渐近的过程,不经过几万行代码的洗礼,是很难成为一个优秀的程序员的,为了帮助初学者找一些好的练习基本功的例子,我近期会整理我讲课一来的一些Python练习程序分享给大家,想学好Python的同学可以照着例子一一去做,我敢保证,把我列出的练习程序列