Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bc242ad

Browse files
feat:add typescript, rust, cpp solutions for 238
1 parent e664016 commit bc242ad

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> productExceptSelf(vector<int> &nums)
5+
{
6+
vector<int> dpLeft(nums.size(), 1);
7+
vector<int> dpRight(nums.size(), 1);
8+
for (int i = 1; i < nums.size(); i++)
9+
{
10+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
11+
}
12+
for (int i = nums.size() - 2; i >= 0; i--)
13+
{
14+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
15+
}
16+
vector<int> result;
17+
for (int i = 0; i < nums.size(); i++)
18+
{
19+
result.push_back(dpLeft[i] * dpRight[i]);
20+
}
21+
return result;
22+
}
23+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
3+
let mut dp_left=vec![1_i32;nums.len()];
4+
let mut dp_right=vec![1_i32;nums.len()];
5+
for i in 1..nums.len(){
6+
dp_left[i]=dp_left[i-1]*nums[i-1];
7+
}
8+
for i in (0..(nums.len()-1)).rev(){
9+
dp_right[i]=dp_right[i+1]*nums[i+1];
10+
}
11+
dp_left.into_iter().enumerate().map(|(i,x)| x*dp_right[i]).collect()
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function productExceptSelf(nums: number[]): number[] {
2+
let dpLeft = Array(nums.length).fill(1);
3+
let dpRight = Array(nums.length).fill(1);
4+
for (let i = 1; i < nums.length; i++) {
5+
dpLeft[i] = dpLeft[i - 1] * nums[i - 1];
6+
}
7+
for (let i = nums.length - 2; i >= 0; i--) {
8+
dpRight[i] = dpRight[i + 1] * nums[i + 1];
9+
}
10+
return dpLeft.map((x, i) => x * dpRight[i]);
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /