- 浏览: 185732 次
- 性别:
- 来自: 济南
文章分类
最新评论
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].
我们创建一个长度和原数组num[]长度一样的数组result[],先从左边开始,让result[0] = 1; result[1] = 1 * num[0], result[2] = num[0] * num[1], result[n] = num[0] * num[1] ...num[n-1]。然后从右边开始,让result[n] = result[n] * 1; result[n - 1] = num[n] * result[n - 1]; result[0] = num[n] * num[n - 1] .... num[1] * result[0]。这样遍历两遍数组就可以得到结果,代码如下:
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
我们创建一个长度和原数组num[]长度一样的数组result[],先从左边开始,让result[0] = 1; result[1] = 1 * num[0], result[2] = num[0] * num[1], result[n] = num[0] * num[1] ...num[n-1]。然后从右边开始,让result[n] = result[n] * 1; result[n - 1] = num[n] * result[n - 1]; result[0] = num[n] * num[n - 1] .... num[1] * result[0]。这样遍历两遍数组就可以得到结果,代码如下:
public class Solution { public int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; int left = 1; result[0] = 1; for(int i = 1; i < nums.length; i++) { result[i] = left * nums[i - 1]; left *= nums[i - 1]; } int right = 1; for(int j = nums.length - 1; j >= 0; j--) { result[j] *= right; right *= nums[j]; } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 573Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 478The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 437Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 587Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 596Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 432All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 909Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 609Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 705Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 871Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 796You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 736For a undirected graph with tre ...
相关推荐
思路:题目中说不能用除法;用暴力,会超时。 为了计算 除第i个元素的乘积,可以将数组分为三个部分:1…i-1; i; i+1…n (注:这里的i代表序号,不是数组下标) output[i] = a * b[被乘数a:代表前i-1个数的乘积...
python python_leetcode题解之238_Product_of_Array_Except_Self.py
Product of Array Except Self 189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All ...
- Product of Array Except Self(数组中除了自身以外的乘积) - Partition Array(分割数组) - First Missing Positive(缺失的第一个正数) - 2 Sum(两数之和) - 3 Sum(三数之和) - 3 Sum Closest(三...
lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 ...of ...of ...Array ...Product of Array Except Self 16 Valid Parenthesis String 17 Number of Islands 18 M
Leetcode的ac是什么意思 LeetCodeInJava ...Product of Array Except Self #242 Valid Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P
13. **Product of Array Except Self**:不包含自身的数组乘积。可以使用前缀和和后缀乘积的概念,结合位运算优化计算。 14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树...
Product of Array Except Self 697. Degree of an Array 849. Maximize Distance to Closest Person ————————————————————————————————————————————————
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...
这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 ...//leetcode....
of TPrinter except some details. In TPrinter Printer.Canvas.Handle and Printer.Handle is the same but in TPrinterPreview PrinterPreview.Canvas.Handle represent the metafile in that is recored the ...