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. 两个事务 update同一张表出现的死锁问题 (转载)

    引言 近来做省一级计算机一级考试系统的时候,学生端进行大批量判分的时候,出现了这样的问题(事务(进程 ID 262)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品.请重新运行该事务.): 这 ...

  2. 关于restful开发的疑惑

    if  你没有了解过restful  return; 一.疑惑 restful风格开发是有争议的,restful的设计是请求“resource”,然后只能对“resource”做CRUD操作.抽象于这 ...

  3. Git IDEA Move or commit them before merge

    提交代码遇到这个问题. Move or commit them before merge 百度了一下都是在Gitbash 中敲命令. 在团队协作中 你总不能去敲命令吧 后来在组长的怂恿下,我删除了一个 ...

  4. 不可变字符串String与可变字符串StringBuilder、StringBuffer使用详解

    String字符串 char类型只能表示一个字符,而String可以表示字符串,也就是一个字符序列.但String不是基本类型,而是一个定义好的类,是一个引用类型.在Java中,可以将字符串直接量赋给 ...

  5. php的mysqli_connect函数显示 No such file or directory错误以及localhost换成127.0.0.1执行成功

    Centos7环境-php7-MariaDB5.5.60 (新安装的php7,执行php -m 显示有mysqli模块,php.ini没有改其它) 测试代码为: <?php //~ echo d ...

  6. JavaScript的迭代函数与迭代函数的实现

    前言 ​ 如果对技术很自信,请直接看 实现的源码 ​ 如果想回顾一下基础,请按文章顺序阅读 说到迭代方法,最先想到的是什么?forEach还是map,迭代的方法ES5提供了5种方法 以下定义来自 Ja ...

  7. PL/SQL报错:ORA-28000:the account is locked

    第一种方法(图形操作):第一步:使用PL/SQL,登录名为system,选择类型的时候把Normal修改为SYSDBA:第二步:选择users下的system,右击点击“编辑”: 第三步:修改密码,把 ...

  8. Webservice返回SoapHeader

    Webservice在请求中加入自定义的SoapHeader,比较常用的场景是在SoapHeader中携带用户登陆信息,由服务端进行身份验证.今天遇到一个需求,除了在请求时要携带SoapHeader外 ...

  9. 20155308 2016-2017-2 《Java程序设计》第10周学习总结

    20155308 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络概述 计算机网络:通过一定的物理设备将处于不同位置的计算机连接起来组成的网络,这个网络 ...

  10. 20155310第一周JAVA实验报告

    20155310第一周JAVA实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 使用JDK和IDE编译.运行简单 ...