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. 苹果IPad客户端安装测试软件

    背景: 公司在开发一个App应用,需要部署在苹果IPad上进行测试,但是我负责后端开发对安装及测试相关流程不了解.经过一番学习得出以下结论: 1. 首先申请一个苹果的开发账号(一千块左右),大约能注册 ...

  2. Lucene补充

    1. 课程计划 Lucene的Field Lucene的索引库维护 lucene的查询 a) Query子对象 b) QueryParser 4.Lucene相关度排序(了解) 2. Field域 2 ...

  3. java获取服务器基本信息

    实现步骤: (1)创建servlet BrowserServer (2)调用HttpServletRequest对象的getServerName()方法获取服务器名称 (3)调用HttpServlet ...

  4. MySQL的库表详细操作

    MySQL数据库 本节目录 一 库操作 二 表操作 三 行操作 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf8; 1.2 数据库命名规则 ...

  5. PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unknow.....

    这个错可能是mysql默认使用 caching_sha2_password作为默认的身份验证插件,而不再是 mysql_native_password,但是客户端暂时不支持这个插件导致的. 解决方法一 ...

  6. linux 学习第十三天(screen不间断会话、apache服务、SELinux安全子系统)

    一.screen 命令不间断会话 1.安装screen(从系统镜像作为yum仓库安装) 1.1.加载系统镜像 1.2.mount /dev/cdrom /media/cdrom/  (挂在系统镜像) ...

  7. aiohttp爬虫的模板,类的形式

    import asyncio import aiohttp import async_timeout from lxml import html from timeit import default_ ...

  8. 课程 python 文件操作复习

    # 文件处理 # 打开文件 #open('路径','打开方式','指定编码方式') # 打开方式 r w a r+ w+ a+ b #r+ 打开文件直接写 和读完再写 # 编码方式 —— utf-8 ...

  9. 课上实践练习——MySort

    模拟实现Linux下Sort -t : -k 2的功能.参考 Sort的实现.提交码云链接和代码运行截图. Linux下Sort -t : -k 2的功能 sort的工作原理: sort将文件的每一行 ...

  10. struts常用知识

    一,struts2是什么? struts2是一个控制框架,相当于连接底层和显示层,控制页面和数据展示 二,为什么用struts2? jsp+javabean模式:jsp里的小脚本java代码太多,页面 ...