剑指offer
构建乘积数组
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:271564
本题知识点:
数组
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2];)
对于A长度为1的情况,B无意义,故而无法构建,因此该情况不会存在。
示例1
输入
复制
[1,2,3,4,5]
[1,2,3,4,5]
返回值
复制
[120,60,40,30,24]
[120,60,40,30,24]
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.ArrayList; public class Solution { public int[] multiply(int[] A) { } }
class Solution { public: vector
multiply(const vector
& A) { } };
# -*- coding:utf-8 -*- class Solution: def multiply(self, A): # write code here
class Solution { public int[] multiply(int[] A) { // write code here } }
function multiply(array) { // write code here } module.exports = { multiply : multiply };
function multiply(array) { // write code here }
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ func multiply( A []int ) []int { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param A int整型一维数组 # @return int整型一维数组 # class Solution def multiply(A) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ def multiply(A: Array[Int]): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ fun multiply(A: IntArray): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ public int[] multiply (int[] A) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ export function multiply(A: number[]): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ func multiply ( _ A: [Int]) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param A int整型一维数组 * @return int整型一维数组 */ pub fn multiply(&self, A: Vec
) -> Vec
{ // write code here } }
[1,2,3,4,5]
[120,60,40,30,24]