Some leetcode problems
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "leetcode"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "leetcode"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
19
src/lib.rs
Normal file
19
src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pub mod p1;
|
||||||
|
pub mod p2;
|
||||||
|
pub mod p238;
|
||||||
|
|
||||||
|
pub struct Solution;
|
||||||
|
|
||||||
|
// Definition for singly-linked list.
|
||||||
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||||
|
pub struct ListNode {
|
||||||
|
pub val: i32,
|
||||||
|
pub next: Option<Box<ListNode>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListNode {
|
||||||
|
#[inline]
|
||||||
|
fn _new(val: i32) -> Self {
|
||||||
|
ListNode { next: None, val }
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/p1.rs
Normal file
29
src/p1.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
use crate::Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
|
||||||
|
for (i, e1) in nums.clone().iter().enumerate() {
|
||||||
|
for (j, e2) in nums[i + 1..].iter().enumerate() {
|
||||||
|
if e1 + e2 == target {
|
||||||
|
return vec![i as i32, (i + (j + 1)) as i32];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unreachable!() // there is exactly one solution
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_two_sum_1() {
|
||||||
|
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_two_sum_2() {
|
||||||
|
assert_eq!(Solution::two_sum(vec![3, 2, 4], 6), vec![1, 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_two_sum_3() {
|
||||||
|
assert_eq!(Solution::two_sum(vec![3, 3], 6), vec![0, 1]);
|
||||||
|
}
|
||||||
119
src/p2.rs
Normal file
119
src/p2.rs
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
use crate::{ListNode, Solution};
|
||||||
|
|
||||||
|
// Definition for singly-linked list.
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn add_two_numbers(
|
||||||
|
l1: Option<Box<ListNode>>,
|
||||||
|
l2: Option<Box<ListNode>>,
|
||||||
|
) -> Option<Box<ListNode>> {
|
||||||
|
Solution::add_two_numbers_rec(l1, l2, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_two_numbers_rec(
|
||||||
|
l1: Option<Box<ListNode>>,
|
||||||
|
l2: Option<Box<ListNode>>,
|
||||||
|
carry: i32,
|
||||||
|
) -> Option<Box<ListNode>> {
|
||||||
|
Option::Some(Box::new(match (l1, l2) {
|
||||||
|
(None, None) => {
|
||||||
|
if carry > 0 {
|
||||||
|
ListNode {
|
||||||
|
val: 1,
|
||||||
|
next: Option::None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Option::None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(None, Some(a)) => {
|
||||||
|
let mut sum = a.val + carry;
|
||||||
|
let mut new_carry = 0;
|
||||||
|
if sum > 9 {
|
||||||
|
new_carry = 1;
|
||||||
|
sum -= 10;
|
||||||
|
}
|
||||||
|
ListNode {
|
||||||
|
val: sum,
|
||||||
|
next: Solution::add_two_numbers_rec(Option::None, a.next, new_carry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(a), None) => {
|
||||||
|
let mut sum = a.val + carry;
|
||||||
|
let mut new_carry = 0;
|
||||||
|
if sum > 9 {
|
||||||
|
new_carry = 1;
|
||||||
|
sum -= 10;
|
||||||
|
}
|
||||||
|
ListNode {
|
||||||
|
val: sum,
|
||||||
|
next: Solution::add_two_numbers_rec(a.next, Option::None, new_carry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(a), Some(b)) => {
|
||||||
|
let mut sum = a.val + b.val + carry;
|
||||||
|
let mut new_carry = 0;
|
||||||
|
if sum > 9 {
|
||||||
|
new_carry = 1;
|
||||||
|
sum -= 10;
|
||||||
|
}
|
||||||
|
ListNode {
|
||||||
|
val: sum,
|
||||||
|
next: Solution::add_two_numbers_rec(a.next, b.next, new_carry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_add_two_numbers_1() {
|
||||||
|
assert_eq!(
|
||||||
|
i32_from_list_node(Solution::add_two_numbers(
|
||||||
|
list_node_from_array(&[2, 4, 3]),
|
||||||
|
list_node_from_array(&[5, 6, 4])
|
||||||
|
)),
|
||||||
|
807
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_add_two_numbers_2() {
|
||||||
|
assert_eq!(
|
||||||
|
i32_from_list_node(Solution::add_two_numbers(
|
||||||
|
list_node_from_array(&[0]),
|
||||||
|
list_node_from_array(&[0])
|
||||||
|
)),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_add_two_numbers_3() {
|
||||||
|
assert_eq!(
|
||||||
|
i32_from_list_node(Solution::add_two_numbers(
|
||||||
|
list_node_from_array(&[9, 9, 9, 9, 9, 9, 9]),
|
||||||
|
list_node_from_array(&[9, 9, 9, 9])
|
||||||
|
)),
|
||||||
|
10009998
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_node_from_array(arr: &[i32]) -> Option<Box<ListNode>> {
|
||||||
|
Option::Some(Box::new(ListNode {
|
||||||
|
val: arr[0],
|
||||||
|
next: if arr.len() > 1 {
|
||||||
|
list_node_from_array(&arr[1..])
|
||||||
|
} else {
|
||||||
|
Option::None
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn i32_from_list_node(l: Option<Box<ListNode>>) -> i32 {
|
||||||
|
if let Some(a) = l {
|
||||||
|
a.val + i32_from_list_node(a.next) * 10
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/p238.rs
Normal file
34
src/p238.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use crate::Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
|
||||||
|
let mut res = nums.clone();
|
||||||
|
let mut acc = 1;
|
||||||
|
for i in 0..nums.len() - 1 {
|
||||||
|
acc *= nums[i];
|
||||||
|
res[i + 1] = acc;
|
||||||
|
}
|
||||||
|
acc = 1;
|
||||||
|
for i in (1..nums.len()).rev() {
|
||||||
|
acc *= nums[i];
|
||||||
|
res[i - 1] *= acc;
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_product_except_self_1() {
|
||||||
|
assert_eq!(
|
||||||
|
Solution::product_except_self(vec![1, 2, 3, 4]),
|
||||||
|
vec![24, 12, 8, 6]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_product_except_self_2() {
|
||||||
|
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