LeetCode——Product of Array Except Self
Description:
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.)
public class Solution {
public int[] productExceptSelf(int[] nums) {
int[] res = new int[nums.length];
res[nums.length - 1] = 1;
for(int i=res.length-2; i>=0; i--) {
res[i] = nums[i+1] * res[i+1];
}
int l = 1;
for(int i=0; i<nums.length; i++) {
res[i] *= l;
l *= nums[i];
}
return res;
}
}
LeetCode——Product of Array Except Self的更多相关文章
- [LeetCode] 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 ...
- 238. [LeetCode] Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- LeetCode -- Product of Array Except Self My Submissions Question
Question: Given an array of n integers where n > 1, nums, return an array output such that output ...
- LeetCode: Product of Array Except Self
Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...
- LeetCode Product of Array Except Self (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- 【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 ...
- 【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 ...
随机推荐
- linux性能评估与分析工具
linux是一个开源系统,其内核负责管理系统的进程,内存,设备驱动程序,文件和网络系统, 决定着系统的性能和稳定性.由于内核源码很容易获取,任何人都可以将自己认为优秀的代码 加入到其中.linux默认 ...
- JAVA里使用CKEditor和CKFinder的配置
在JSP里使用CKEditor和CKFinder的配置 CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传. “预览”中有一大堆鸟语,看 ...
- unsupported major.monor version 51.0 (unable to load *.servlet)………………
unsupported major.monor version 51.0 (unable to load *.servlet)------ 这种异常是因为编译war或者java程序的JDK版本和运行部 ...
- yarn是什么?为什么会产生yarn,它解决了什么问题?以及yarn的执行流程
yarn是什么?为什么会产生yarn,它解决了什么问题? 答:yarn是作业调度和集群资源管理的一个框架. 首先对之前的Hadoop 和 MRv1 简单介绍如下: Hadoop 集群可从单一节点 ...
- 树莓派 安装 刷Android Things 小结
一句话说,Android Things就是让开发者可以使用Android开发工具开发嵌入式设备. If you can build an app, you can build a device. 只要 ...
- java英文缩写
JSR Java Specification Request Java规范请求 POJO Plain Old Java Object 简单的Java对象
- ASP.NET MVC 使用 Datatables (2)
在服务器端实现分页,排序,获取当前页面数据 在上篇的基础上进行改造(datatables的客户端实现) 1.修改View页面代码如下: <div class="row"> ...
- (三)使用预定义模型QDirModel的例子
使用预定义模型QDirModel的例子 Main.cpp #include <QApplication> #include "directoryviewer.h" in ...
- tensorflow学习笔记(10) mnist格式数据转换为TFrecords
本程序 (1)mnist的图片转换成TFrecords格式 (2) 读取TFrecords格式 # coding:utf-8 # 将MNIST输入数据转化为TFRecord的格式 # http://b ...
- 【转】OPenGL MFC绘图
一.简介 GDI是通过设备句柄(Device Context以下简称"DC")来绘图,而OpenGL则需要绘制环境(Rendering Context,以下简称"RC&q ...