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.)
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size(),); int i;
for(i=;i<nums.size();i++)
{
res[i]=res[i-]*nums[i-];
}
int right=;
for(i=nums.size()-;i>=;i--)
{
res[i]*=right;
right*=nums[i];
}
return res;
}
};
Product of Array Except Self的更多相关文章
- [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 ...
- 【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 ...
- 【08_238】Product of Array Except Self
Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...
- 【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 ...
- LeetCode OJ 238. Product of Array Except Self 解题报告
题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...
- 238. Product of Array Except Self(对O(n)和递归又有了新的理解)
238. Product of Array Except Self Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...
- leetcode:238. Product of Array Except Self(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...
- 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 ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
随机推荐
- JavaScript SetInterval与setTimeout使用方法详解
setTimeout和setInterval的语法相同.它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码.不过这两个函数还是有区别的 ...
- js中的==运算: [''] == false —>true
图1 计算下面表达式的值: [''] == false 首先,两个操作数分别是对象类型.布尔类型.根据图1,需要将布尔类型转为数字类型,而false转为数字的结果是0,所以表达式变为: [''] == ...
- Use Excel Pivot Table as a BI tool
Normally, we have created a table, view in database or cube in SSAS, user can use Excel as a BI tool ...
- java统计汉字
public class TotalUtil { public static int getSum(String text) { String reg = "^[\u4e00- ...
- PHP非阻塞模式 (转自 尘缘)
让PHP不再阻塞当PHP作为后端处理需要完成一些长时间处理,为了快速响应页面请求,不作结果返回判断的情况下,可以有如下措施: 一.若你使用的是FastCGI模式,使用fastcgi_finish_re ...
- MySQL创建数据库和表的Demo
Demo: 创建数据库的语法 1.基本语法 create database tour character set gbk; use tour; 无主键自增长的 create table EMB_T_E ...
- ipcs, ipcrm
ipcs ipcs -m #查看系统中已经存在的共享内存 ------ Shared Memory Segments -------- key shmid owner perms bytes natt ...
- (转)Yii的组件机制之一:组件基础类CComponent分析
Yii的组件机制 组件机制,是Yii整个体系的思想精髓,在使用Yii之前,最应该先了解其组件机制,如果不了解这个机制,那么阅读Yii源代码会非常吃力.组件机制给Yii框架赋予了无穷的灵活性和可扩展性, ...
- linux基础-基本命令的讲解(1-7单元)
基本命令的讲解 主要内容介绍 1.LINUX操作系统安装及初始化配置(熟悉):2.LINUX操作系统目录组成结构及文件级增删改查操作(重点):3.LINUX操作系统用户.权限管理(重点):4.开源软件 ...
- Bootstrap学习(3)
Bootstrap 图片 Bootstrap 对图片的支持.Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来 ...