From 5da5a2ca0a16c1813e16d272ab022b27b4000569 Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 13 Apr 2024 16:32:01 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + .vscode/settings.json | 5 ++++ Cargo.lock | 7 ++++++ Cargo.toml | 8 +++++++ src/bin/1_two_sum.rs | 16 +++++++++++++ src/bin/2_add_two_numbers.rs | 24 +++++++++++++++++++ ..._substring_without_repeating_characters.rs | 20 ++++++++++++++++ src/bin/4_median_of_two_sorted_arrays.rs | 8 +++++++ 8 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/bin/1_two_sum.rs create mode 100644 src/bin/2_add_two_numbers.rs create mode 100644 src/bin/3_longest_substring_without_repeating_characters.rs create mode 100644 src/bin/4_median_of_two_sorted_arrays.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..352a626 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "rust-analyzer.linkedProjects": [ + "./Cargo.toml" + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d5eb8a3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "leetcode" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b3979a3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "leetcode" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/bin/1_two_sum.rs b/src/bin/1_two_sum.rs new file mode 100644 index 0000000..17f9032 --- /dev/null +++ b/src/bin/1_two_sum.rs @@ -0,0 +1,16 @@ +use std::collections::HashMap; + +fn main() { + println!("{:?}", two_sum(vec![2,7,11,15], 9)); +} + +pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut components: HashMap = HashMap::new(); + for i in 0..nums.len() { + let component = target - nums[i]; + let j = components.get(&component); + if j.is_some() { return vec![i as i32, *j.unwrap() as i32]; } + components.insert(nums[i], i); + } + return Vec::new(); +} diff --git a/src/bin/2_add_two_numbers.rs b/src/bin/2_add_two_numbers.rs new file mode 100644 index 0000000..065bf43 --- /dev/null +++ b/src/bin/2_add_two_numbers.rs @@ -0,0 +1,24 @@ +fn main() { + +} + +pub fn add_two_numbers(l1: Option>, l2: Option>) -> Option> { + return None; +} + +// Definition for singly-linked list. +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option> +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { + next: None, + val + } + } +} diff --git a/src/bin/3_longest_substring_without_repeating_characters.rs b/src/bin/3_longest_substring_without_repeating_characters.rs new file mode 100644 index 0000000..8a83af0 --- /dev/null +++ b/src/bin/3_longest_substring_without_repeating_characters.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +fn main() { + println!("{}", length_of_longest_substring(String::from("abcabcbb"))); +} + +pub fn length_of_longest_substring(s: String) -> i32 { + let mut seen: HashMap = HashMap::with_capacity(80); + let (mut start_index, mut max_length) = (0usize, 0i32); + for (i, c) in s.chars().enumerate() { + if let Some(index) = seen.get(&c) { + max_length = max_length.max((i - start_index) as i32); + start_index = *index + 1; + seen.retain(|_, i| *i >= start_index); + } + seen.insert(c, i); + } + max_length = max_length.max((s.len() - start_index) as i32); + return max_length; +} diff --git a/src/bin/4_median_of_two_sorted_arrays.rs b/src/bin/4_median_of_two_sorted_arrays.rs new file mode 100644 index 0000000..dc8a3fc --- /dev/null +++ b/src/bin/4_median_of_two_sorted_arrays.rs @@ -0,0 +1,8 @@ +fn main() { + +} + +pub fn find_median_sorted_arrays(nums1: Vec, nums2: Vec) -> f64 { + + return 0.0; +}