mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-08 06:07:27 -06:00
Move all solutions into separate directory (cleanup)
the beginning
This commit is contained in:
33
solutions/product-of-array-except-self/src/main.rs
Normal file
33
solutions/product-of-array-except-self/src/main.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
|
||||
let mut result = vec![1; nums.len()];
|
||||
let mut left = 1;
|
||||
let mut right = 1;
|
||||
|
||||
for i in 0..nums.len() {
|
||||
// Moving left to right: multiple each number by the preceding number
|
||||
result[i] *= left;
|
||||
left *= nums[i];
|
||||
|
||||
// Moving right to left, multiple each number by the following number
|
||||
result[nums.len() - 1 - i] *= right;
|
||||
right *= nums[nums.len() - 1 - i];
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(
|
||||
Solution::product_except_self(vec![1, 2, 3, 4]),
|
||||
vec![24, 12, 8, 6]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
Solution::product_except_self(vec![-1, 1, 0, -3, 3]),
|
||||
vec![0, 0, 9, 0, 0]
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user