238. Product of Array Except Self
来源:互联网 发布:新西兰林肯大学 知乎 编辑:程序博客网 时间:2024/06/10 20:16
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.)
class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { int n=nums.size(); vector<int> front(n); vector<int> back(n); front[0]=1; back[n-1]=1; for(int i=1;i<n;i++){ front[i]=front[i-1]*nums[i-1]; back[n-1-i]=back[(n-1-i)+1]*nums[(n-1-i)+1]; } //vector<int> res;报错,由于后面直接调用res[i],而这里并没有声明长度 vector<int> res(n); for(int i=0;i<n;i++){ res[i]=front[i]*back[i]; } return res; }};
阅读全文
0 0
- [leetcode] 238.Product of Array Except Self
- [LeetCode]238.Product of Array Except Self
- [leetcode] 238. Product of Array Except Self
- Leetcode 238. Product of Array Except Self
- [LeetCode]238. Product of Array Except Self
- 238. Product of Array Except Self LeetCode
- LeetCode 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- [LeetCode]238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- 238. Product of Array Except Self
- LeetCode *** 238. Product of Array Except Self
- LeetCode 238. Product of Array Except Self
- tensorflow 指定GPU
- eclipse的使用技巧小结
- spring boot之初体验
- Maven之Maven Surefire Plugin(JUnit篇)
- 关于json_decode()返回NULL
- 238. Product of Array Except Self
- include、import、@class区别
- c#.net中如何实现将鼠标放在按钮上,出现提示文字?
- 新零售业务中台设计及产品体系解决方案
- [LeetCode 386] Lexicographical Numbers(Python)
- 测试 《 Windows环境下实现Nginx+2tomcat实现负载均衡》
- File类访问文件2~杀毒
- oracle数据中记录被另一个用户锁住 (附加:操作实例)
- vsftpd 添加用户 指定目录 禁止访问上级目录