【08_238】Product of Array Except Self
Product of Array Except Self
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]
.
Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)
限定了不能用除法,和时间复杂度。
但Follow up我没太懂,是不允许申请额外的空间吗?
这次本来想用位运算中的异或运算写,但是发现那样有固有缺陷——对负数不起效果。而且越改越臃肿。
最后在Discuss中找到一个好方法, 这个方法申请了额外的数组。
优点:
- 得到数组中新的一个数字时,借助了上一位,从而不用再次遍历整个数组
- 方法很巧妙
下面是看完解答后自己写的:
Java语言写的
public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[0] = 1;
for (int i = 1; i < nums.length; i++) {
res[i] = res[i - 1] * nums[i - 1];
}
int right = 1;
for (int i = nums.length - 1; i >= 0; i--) {
res[i] *= right;
right *= nums[i];
}
return res;
}
}
【08_238】Product of Array Except Self的更多相关文章
- 【leetcode81】Product of Array Except Self
题目描述: 给定一个长度为n的整数数组Array[],输出一个等长的数组result[],这个输出数组,对应位置i是除了Array[i]之外,其他的所有元素的乘积 例如: given [1,2,3,4 ...
- 【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 ...
- 【数组】Product of Array Except Self
题目: iven an array of n integers where n > 1, nums, return an array output such that output[i] is ...
- 【02】[].slice和Array.prototype.slice
[02][].slice和Array.prototype.slice 01,Array是一个构造函数.浏览器内置的特殊对象. 02,Array没有slice方法. 03,Array.prototy ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode 238】Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- 【转载】Java集合类Array、List、Map区别和联系
Java集合类主要分为以下三类: 第一类:Array.Arrays第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Arrays ...
随机推荐
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
- myeclipse搭建SSH框架
搭建SSH框架 Struts+hibernater+spring架构(myeclipse) 右击,首先加入spring,加入hibernater,再加入struts2 复制jar包(把tomcat发布 ...
- 链接测试工具xenu link sleuth的使用
链接测试工具xenu link sleuth的使用很简单. 可以从这里下载 http://home.snafu.de/tilman/xenulink.html 但是注意到: 如果需要登录才能进入所有的 ...
- 获取body标签元素方法
方法一 doucumnet.body 方法二 document.getElementsByTagName("body")[0]
- 嵌入式Linux内核I2C子系统详解
1.1 I2C总线知识 1.1.1 I2C总线物理拓扑结构 I2C总线在物理连接上非常简单,分别由SDA(串行数据线)和SCL(串行时钟线)及上拉电阻组成.通信原理是通过对SCL和SDA线高 ...
- MySQL计算销售员昨日各指标综合得分_20161206
数据需求如上,新增重激活以15天未下单为界定 SELECT d.销售员,(日销售额得分*0.6+日新增客户数得分*0.15+日客单价得分*0.1+日客户平均产品数得分*0.15)AS 综合得分,日销售 ...
- CSS声明顺序
CSS对元素样式进行声明,虽然一条规则中的声明可以按照任何顺序写出来,但是还是应该有个优先级的顺序. 如下的声明顺序摘抄自<CSS设计指南>,优先级顺序如下: display及相关声明 p ...
- C++ 一个程序获取另一个程序Edit控件的内容
//一个程序获取另一个程序Edit控件的内容 //根据指定程序的标题名获取改程序窗口的句柄 HWND hWnd=::FindWindow(NULL,"zhang001"); if( ...
- JavaScript初学者应注意的七个细节(转)
http://www.cnblogs.com/lhb25/archive/2011/01/10/1932284.html 每种语言都有它特别的地方,对于JavaScript来说,使用var就可以声明任 ...
- g++与c++扩栈方法
g++: /* * Problem: * Author: SHJWUDP * Created Time: 2015/8/5 星期三 15:54:42 * File Name: tmp.cpp * St ...