Initial commit

This commit is contained in:
Egor 2024-04-13 16:32:01 +03:00
commit 5da5a2ca0a
8 changed files with 89 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
]
}

7
Cargo.lock generated Normal file
View file

@ -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"

8
Cargo.toml Normal file
View file

@ -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]

16
src/bin/1_two_sum.rs Normal file
View file

@ -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<i32>, target: i32) -> Vec<i32> {
let mut components: HashMap<i32, usize> = 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();
}

View file

@ -0,0 +1,24 @@
fn main() {
}
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
return None;
}
// 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
}
}
}

View file

@ -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<char, usize> = 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;
}

View file

@ -0,0 +1,8 @@
fn main() {
}
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
return 0.0;
}