【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 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.)
题意:
给定一个数组,对于数组中每一个元素i,求ret[i] = nums[0]*nums[1]...*nums[i-1]*nums[i+1]*...*nums[len-1]。不能用除法,要求时间复杂度O(n).
思路:
先由左到右的顺序求每个元素左边乘积(ret[i] = nums[0]*nums[1]...*nums[i-1]),在其结果上由右到左求每个元素右边乘积(ret[i] = ret[i]*nums[i+1]*...*nums[len-1])即可,其中nums[0]*nums[1]...*nums[i-1]以及nums[i+1]*...*nums[len-1]都可由常量连乘得到。
C++:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int len = nums.size(), temp;
vector<int> ret(len, );
temp = ;
for(int i = ; i < len; i++)
{
temp *= nums[i - ];
ret[i] *= temp;
}
temp = ;
for(int i = len - ; i >= ; i--)
{
temp *= nums[i + ];
ret[i] *= temp;
}
return ret;
}
};
【LeetCode 238】Product of Array Except Self的更多相关文章
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- 【leetcode❤python】 189. Rotate Array
#-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
随机推荐
- css属性
文本: color 颜色 line-height 行高 direction 文本方向 letter-spacing 字符间距 text-align 对齐方式 text-decoration 文本修饰 ...
- .NET中使用OleDb读取Excel
在.NET中可以用OleDb(Object linking and embeding DataBase)来访问Excel using System; using System.Data; using ...
- ehcache 缓存技术
一 ehcache API: 1: Using the CacheManager 1.1所有ehcache的使用, 都是从 CacheManager. 开始的.有多种方法创建CacheManager实 ...
- 解决 emulator-5554 disconnected !Cancelling错误
http://www.xuebuyuan.com/351215.html 使用Android模拟器经常遇到连不上.连一次掉一次等诸多问题(转载+原创) 解决办法一: 在此种情形下,重启ADB即可: 1 ...
- 快速获取Windows系统上的国家和地区信息
Windows系统上包含了200多个国家和地区的数据,有时候编程需要这些资料.以下代码可以帮助你快速获取这些信息.将Console语句注释掉,可以更快的完成分析. static void Main(s ...
- c++异常 连续抛出异常
今天天遇到这样一个问题,连续两次抛出异常,但是只有一个catch,会导致core这个时候会导致core, 单线程编程中可能很少遇到这样的问题,但是多线程中是很容易遇到的, 举个例子:catch代码 ...
- H5移动前端完美布局之padding
序上次的提到了H5移动前端完美布局之-margin百分比的使用margin-top(left,right,bottom)的百分比在移动页面布局中对上下左右距离的处理,攻下城外再攘城内,今天看看padd ...
- R语言屏幕输出
cat("the total number is:",3+5,"\n") print(x, ...) ?print?cat?format ?write
- 简单了解JAVA8的新特性
JAVA8新特性会颠覆整个JAVA程序员的编程习惯 甚至如果您坚守JAVA7之前的编程习惯,今后你看比较年轻的程序员写的JAVA代码都会无法理解 所以为了保证不脱钩,我觉得有必要学习JAVA8的新特性 ...
- ASP.NET MVC路由配置
一.命名参数规范+匿名对象 routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}" ...