Given an integers array A.

Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.

Example

For A = [1, 2, 3], return [6, 3, 2].

分析:

 public class Solution {
public int[] productExceptSelf(int[] A) {
int[] left = new int[A.length];
int[] right = new int[A.length];
int[] result = new int[A.length]; for (int i = ; i < A.length; i++) {
left[i] = i == ? : left[i - ] * A[i - ];
right[A.length - - i] = (i == ) ? : right[A.length - i] * A[A.length - i];
} for (int i = ; i < A.length; i++) {
result[i] = left[i] * right[i];
}
return result;
}
}
 public class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] res = new int[n];
res[] = ;
for (int i = ; i < n; i++) {
res[i] = res[i - ] * nums[i - ];
}
int right = ;
for (int i = n - ; i >= ; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}

one pass

 class Solution {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
Arrays.fill(result, );
int left = , right = ;
for (int i = , j = nums.length - ; i < nums.length - ; i++, j--) {
left *= nums[i];
right *= nums[j];
result[i + ] *= left;
result[j - ] *= right;
}
return result;
}
}

Product of Array Exclude Itself的更多相关文章

  1. Lintcode: Product of Array Exclude Itself

    Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B wi ...

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

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

  4. 【08_238】Product of Array Except Self

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

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

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

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

  7. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  8. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

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

随机推荐

  1. grid-css

    .fil-container { width: 100%; max-width: 75rem; margin-right: auto; margin-left: auto; padding-left: ...

  2. python 切片

    本人的博客中的python内容基本上全是看着廖雪峰大神博客做的一个笔记 关于列表或者元祖的切片 下面说一下列表的切片的语法: L[起始位置:结束位置:步长] number = range(100) n ...

  3. iOS边练边学--cocoaPods管理第三方框架--命令行方式实现

    更换源 Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/)源来查找.安装.升级和写在软件包 gem sources --remove ht ...

  4. 小菜鸟学 Spring-Dependency injection(二)

    注入方式一:set注入 <bean id="exampleBean" class="examples.ExampleBean"> <!-- s ...

  5. unicode和gbk的互相转换

    unicode和gbk的互相转换主要依靠window下的escape和unescape方法,然后把%u替换成\u就好了; var GB2312UnicodeConverter = { ToUnicod ...

  6. hdu2222 AC自动机

    字典树也可以做. #include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 1000 ...

  7. Java-list,set,map的区别

    jdk中api的定义 Collection ├----List │ ├----LinkedList │ ├----ArrayList │ └----Vector │ └----Stack └----S ...

  8. 【Matplotlib】图例分开显示

    作图时图例往往都会出现一个图例框内,如果需要不同类型的图例分别显示,比如显示两个图例. 基本上,出现两个图例的话,需要调用两次 legend .第一次调用,你需要将图例保存到一个变量中,然后保存下来. ...

  9. TOMOYO Linux(undone)

    目录 . TOMOYO Introduction . TOMOYO Sourcecode Analysis 1. Introduction TOMOYO是一款基于LSM Framework实现的LSM ...

  10. 连通性1 求无向图的low值

    这是DFS系列的第一篇 . 首先给出一个重要的定理.该定理来自<算法导论>. An undirected graph may entail some ambiguity in how we ...