238. Product of Array Except Self
2016-06-23 21:33
344 查看
Given an array of n integers where n > 1,
return an array
equal to the product of all the elements of
Solve it without division and in O(n).
For example, given
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.)
我的解答:
1.额外空间O(n) 时间复杂度O(1)
2.额外空间O(1) 时间复杂度O(1)
//
// main.cpp
// 238. Product of Array Except Self
//
// Created by zjl on 16/6/23.
// Copyright © 2016年 zjl. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
vector<int> productExceptSelf1(vector<int>& nums) {
int n = nums.size();
vector<int> formbeg(n+1,1);
vector<int> formend(n+1,1);
vector<int> res(n,1);
for(int i = 1; i <= n; i++){
formbeg[i] = formbeg[i-1]*nums[i-1];
}
for(int i = n-1; i >=0; i--){
formend[i] = formend[i+1]*nums[i];
}
for(int j = 0; j < n; j++){
res[j] = formbeg[j] * formend[j+1];
}
return res;
}
vector<int> productExceptSelf2(vector<int>& nums) {
int n = nums.size();
vector<int>res(n,1);
for(int i = 1; i < n; i++){
res[i] = res[i-1]*nums[i-1];
}
long long t = 1;
for(int j = n-1; j >=0; j--){
res[j] *= t;
t *= nums[j];
}
return res;
}
int main(int argc, const char * argv[]) {
vector<int> nums = {1,2,3,4};
vector<int> res = productExceptSelf2(nums);
for(int i = 0; i < res.size(); i++)
cout<<res[i] << " ";
cout<<endl;
return 0;
}
输入: 1,2,3,4
输出:24 12 8 6
nums,
return an array
outputsuch that
output[i]is
equal to the product of all the elements of
numsexcept
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.)
我的解答:
1.额外空间O(n) 时间复杂度O(1)
2.额外空间O(1) 时间复杂度O(1)
//
// main.cpp
// 238. Product of Array Except Self
//
// Created by zjl on 16/6/23.
// Copyright © 2016年 zjl. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
vector<int> productExceptSelf1(vector<int>& nums) {
int n = nums.size();
vector<int> formbeg(n+1,1);
vector<int> formend(n+1,1);
vector<int> res(n,1);
for(int i = 1; i <= n; i++){
formbeg[i] = formbeg[i-1]*nums[i-1];
}
for(int i = n-1; i >=0; i--){
formend[i] = formend[i+1]*nums[i];
}
for(int j = 0; j < n; j++){
res[j] = formbeg[j] * formend[j+1];
}
return res;
}
vector<int> productExceptSelf2(vector<int>& nums) {
int n = nums.size();
vector<int>res(n,1);
for(int i = 1; i < n; i++){
res[i] = res[i-1]*nums[i-1];
}
long long t = 1;
for(int j = n-1; j >=0; j--){
res[j] *= t;
t *= nums[j];
}
return res;
}
int main(int argc, const char * argv[]) {
vector<int> nums = {1,2,3,4};
vector<int> res = productExceptSelf2(nums);
for(int i = 0; i < res.size(); i++)
cout<<res[i] << " ";
cout<<endl;
return 0;
}
输入: 1,2,3,4
输出:24 12 8 6
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解