Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

Input:  [1,2,3,4]
Output: [24,12,8,6]

Note: Please solve it without division and in O(n).

Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)


注释:用两个指针,前面的指针fromBegin比返回的res少乘一个当前的i,后面的指针也是如此。当i到达结尾时,前后都覆盖到了。


#include <cstdio>
#include <vector>
#include<iostream>
using namespace std; class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
int fromBegin=;
int fromLast=;
vector<int> res(n,); for(int i=;i<n;i++){
res[i]*=fromBegin;
fromBegin*=nums[i];
res[n--i]*=fromLast;
fromLast*=nums[n--i];
}
return res;
}
};
int main(){
vector<int> temp;
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
temp.push_back();
Solution test;
test.productExceptSelf(temp);
return ;
}

238. [LeetCode] Product of Array Except Self的更多相关文章

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

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

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

  4. LeetCode——Product of Array Except Self

    Description: Given an array of n integers where n > 1, nums, return an array output such that out ...

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

  6. LeetCode: Product of Array Except Self

    Dynamic Programming public class Solution { public int[] productExceptSelf(int[] nums) { int[] ans = ...

  7. LeetCode Product of Array Except Self (除自身外序列之积)

    题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...

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

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

  9. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

随机推荐

  1. 结构之美——优先队列基本结构(四)——二叉堆、d堆、左式堆、斜堆

    实现优先队列结构主要是通过堆完成,主要有:二叉堆.d堆.左式堆.斜堆.二项堆.斐波那契堆.pairing 堆等. 1. 二叉堆 1.1. 定义 完全二叉树,根最小. 存储时使用层序. 1.2. 操作 ...

  2. 使用Consul做服务发现的若干姿势

    从2016年起就开始接触Consul,使用的主要目的就是做服务发现,后来逐步应用于生产环境,并总结了少许使用经验.最开始使用Consul的人不多,为了方便交流创建了一个QQ群,这两年微服务越来越火,使 ...

  3. JDBC——释放资源的代码

    public static void release(ResultSet rs, Statement statement, Connection conn) { if (rs != null) { t ...

  4. CentOS6.10安装详解

    一.简介 CentOS(Community Enterprise Operating System,中文意思是社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterpriser ...

  5. SDL2 undefined reference to `SDL_Init' 问题

    我在使用SDL2的时候,遇到undefined reference to `SDL_Init'的问题,只要使用SDL2相关的函数,就会报函数未定义.后来百度到一篇文章https://blog.csdn ...

  6. js 里常用的字符串操作方法

    /*var str='啦啦啦'; var str1='哈哈哈' //charAt() 返回指定索引处的字符串 console.log(str.charAt(3)) //charCodeAt() 返回指 ...

  7. 高性能MySQL--MySQL数据类型介绍和最优数据类型选择

    MySQL支持的数据类型很多,那么选择合适的数据类型对于获得高性能就至关重要.那么就先了解各种类型的优缺点! 一.类型介绍 1.整型类型 整型类型有: TINYINT,SMALLINT,MEDIUMI ...

  8. 解决$ go get google.golang.org/grpc上的包被墙的问题

    今天get grpc包的时候 $ go get google.golang.org/grpc 发现拉不下来被墙了,在github.com上搜索grpc,clone到工程目录中,运行命令 go inst ...

  9. 转 关于window10安装jdk,配置环境变量,javac不是内部或外部命令,也不是可运行的程序 或批处理文件的细节问题。

    今日拿到一台新的window10笔记本电脑,非常熟练的安装了JDK(因为在学校经常给同学安装JDK - -)但是发现java java -version命令都可以使用,唯独javac命令出现不是内部或 ...

  10. python中Excel表操作

    python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls ...