题目:

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

代码:

  1. class Solution {
  2. public:
  3. vector<int> productExceptSelf(vector<int>& nums) {
  4. int n = nums.size();
  5. vector<int> fromBegin(n);
  6. vector<int> fromLast(n);
  7. fromBegin[] = fromLast[] = ;
  8. for(int i = ; i < n; ++i)
  9. {
  10. fromBegin[i] = fromBegin[i-] * nums[i-];
  11. fromLast[i] = fromLast[i-] *nums[n-i];
  12. }
  13. vector<int> res(n);
  14. for(int i = ; i < n; ++i)
  15. {
  16. res[i] = fromBegin[i]*fromLast[n-i-];
  17. }
  18. return res;
  19. }
  20. };

[LeetCode238]Product of Array Except Self的更多相关文章

  1. Leetcode238. Product of Array Except Self除自身以外数组的乘积

    给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...

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

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

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

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

  5. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

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

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

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

  8. 238. Product of Array Except Self(对O(n)和递归又有了新的理解)

    238. Product of Array Except Self     Total Accepted: 41565 Total Submissions: 97898 Difficulty: Med ...

  9. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

随机推荐

  1. TRIZ系列-创新原理-29-气动或液压结构原理

    气动或液压结构原理的详细表述例如以下:1)用气态或液态部件替代固体部件.能够用空气或者水,也能够用气垫或水垫,使这些部件膨胀.这条原理符合系统的动态性进化法则-柔性化.在改造系统时,我们能够尝试将系统 ...

  2. Everything You Wanted to Know About Machine Learning

    Everything You Wanted to Know About Machine Learning 翻译了理解机器学习的10个重要的观点,增加了自己的理解.这些原则在大部分情况下或许是这样,可是 ...

  3. Hive Metastore ObjectStore PersistenceManager自动关闭bug解析

    最近在测试HCatalog,由于Hcatalog本身就是一个独立JAR包,虽然它也可以运行service,但是其实这个service就是metastore thrift server,我们在写基于Hc ...

  4. JavaScript 使用Document记录cookie

    cookie对于我们使用者来说,有时帮助还是挺大的,比方对于一些不是特别重要的站点,比方公司的測试平台,每次登陆都要手动输入username和password 非常繁琐.所以为了更少的引入其他框架,就 ...

  5. hdu2036 (计算多边形的面积)

    Input 输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1 ...

  6. SEAndroid安全机制中的进程安全上下文关联分析

    前面一篇文章分析了文件安全上下文关联过程.可是在SEAndroid中,除了要给文件关联安全上下文外,还须要给进程关联安全上下文.由于仅仅有当进程和文件都关联安全上下文之后,SEAndroid安全策略才 ...

  7. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  8. java-IO操作性能对照

    在软件系统中.IO速度比内存速度慢,IO读写在非常多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作.以字节为处理单位:Reade ...

  9. Oracle基础(五)pl/sql进阶(分页过程)

    编写分页过程         通过pl/sql实现分页过程,再该过程中由简单到难一步步深入,目的在于通过该案例熟悉pl/sql的各种存储过程,包,游标.怎样在java中调用等内容的学习. 1.无返回值 ...

  10. 天翼玩家wifi,鸡肋or神器?

    昨天,天一在成都,一个举行4G体验活动.谁是背着一个婴儿每一翼4G MiFi终奌站.市民可进入用户password自由的直接经验wifi互联网. 天翼随身wifi是什么? 这样的4G MiFi就是天翼 ...