Skip to content

Commit 686ed81

Browse files
authored
Added rust
1 parent 8bc6439 commit 686ed81

File tree

201 files changed

+7759
-411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+7759
-411
lines changed

README.md

Lines changed: 411 additions & 411 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
// #2024_08_24_Time_0_ms_(100.00%)_Space_2.4_MB_(23.16%)
4+
5+
use std::collections::HashMap;
6+
7+
impl Solution {
8+
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
9+
let mut index_map: HashMap<i32, usize> = HashMap::new();
10+
11+
for (i, &num) in numbers.iter().enumerate() {
12+
let required_num = target - num;
13+
if let Some(&index) = index_map.get(&required_num) {
14+
return vec![index as i32, i as i32];
15+
}
16+
index_map.insert(num, i);
17+
}
18+
19+
vec![-1, -1]
20+
}
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_08_24_Time_0_ms_(100.00%)_Space_2.2_MB_(14.25%)
4+
5+
// Definition for singly-linked list.
6+
// #[derive(PartialEq, Eq, Clone, Debug)]
7+
// pub struct ListNode {
8+
// pub val: i32,
9+
// pub next: Option<Box<ListNode>>
10+
// }
11+
//
12+
// impl ListNode {
13+
// #[inline]
14+
// fn new(val: i32) -> Self {
15+
// ListNode {
16+
// next: None,
17+
// val
18+
// }
19+
// }
20+
// }
21+
impl Solution {
22+
pub fn add_two_numbers(
23+
l1: Option<Box<ListNode>>,
24+
l2: Option<Box<ListNode>>
25+
) -> Option<Box<ListNode>> {
26+
let mut p = l1;
27+
let mut q = l2;
28+
let mut dummy_head = Box::new(ListNode::new(0));
29+
let mut curr = &mut dummy_head;
30+
let mut carry = 0;
31+
32+
while p.is_some() || q.is_some() {
33+
let x = p.as_ref().map_or(0, |node| node.val);
34+
let y = q.as_ref().map_or(0, |node| node.val);
35+
let sum = carry + x + y;
36+
carry = sum / 10;
37+
curr.next = Some(Box::new(ListNode::new(sum % 10)));
38+
curr = curr.next.as_mut().unwrap();
39+
40+
if let Some(node) = p {
41+
p = node.next;
42+
}
43+
if let Some(node) = q {
44+
q = node.next;
45+
}
46+
}
47+
48+
if carry > 0 {
49+
curr.next = Some(Box::new(ListNode::new(carry)));
50+
}
51+
52+
dummy_head.next
53+
}
54+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
// #Big_O_Time_O(n)_Space_O(1) #2024_08_24_Time_0_ms_(100.00%)_Space_2.3_MB_(28.72%)
4+
5+
impl Solution {
6+
pub fn length_of_longest_substring(s: String) -> i32 {
7+
// Array to store last seen indices of characters
8+
let mut last_indices = [-1; 256];
9+
let mut max_len = 0;
10+
let mut cur_len = 0;
11+
let mut start = 0;
12+
13+
// Convert string to bytes to use indexing
14+
let bytes = s.as_bytes();
15+
for (i, &cur) in bytes.iter().enumerate() {
16+
// Cast byte to usize to use as an index
17+
let cur = cur as usize;
18+
19+
if last_indices[cur] < start as i32 {
20+
last_indices[cur] = i as i32;
21+
cur_len += 1;
22+
} else {
23+
let last_index = last_indices[cur];
24+
start = (last_index + 1) as usize;
25+
cur_len = i - start + 1;
26+
last_indices[cur] = i as i32;
27+
}
28+
29+
if cur_len > max_len {
30+
max_len = cur_len;
31+
}
32+
}
33+
34+
max_len as i32
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2024_08_24_Time_0_ms_(100.00%)_Space_2.2_MB_(39.80%)
3+
4+
impl Solution {
5+
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
6+
let (nums1, nums2) = if nums1.len() > nums2.len() {
7+
// Ensures nums1 is the smaller array
8+
(nums2, nums1)
9+
} else {
10+
(nums1, nums2)
11+
};
12+
13+
let n1 = nums1.len();
14+
let n2 = nums2.len();
15+
let mut low = 0;
16+
let mut high = n1;
17+
18+
while low <= high {
19+
let cut1 = (low + high) / 2;
20+
let cut2 = (n1 + n2 + 1) / 2 - cut1;
21+
22+
let l1 = if cut1 == 0 { i32::MIN } else { nums1[cut1 - 1] };
23+
let l2 = if cut2 == 0 { i32::MIN } else { nums2[cut2 - 1] };
24+
let r1 = if cut1 == n1 { i32::MAX } else { nums1[cut1] };
25+
let r2 = if cut2 == n2 { i32::MAX } else { nums2[cut2] };
26+
27+
if l1 <= r2 && l2 <= r1 {
28+
// Found the correct partition
29+
if (n1 + n2) % 2 == 0 {
30+
return (f64::from(l1.max(l2)) + f64::from(r1.min(r2))) / 2.0;
31+
} else {
32+
return f64::from(l1.max(l2));
33+
}
34+
} else if l1 > r2 {
35+
high = cut1 - 1;
36+
} else {
37+
low = cut1 + 1;
38+
}
39+
}
40+
41+
// Fallback case, should never hit
42+
0.0
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2+
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
3+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
4+
// #2024_08_24_Time_1_ms_(92.60%)_Space_2.2_MB_(20.49%)
5+
6+
impl Solution {
7+
pub fn longest_palindrome(s: String) -> String {
8+
let n = s.len();
9+
if n == 0 {
10+
return String::new();
11+
}
12+
13+
// Step 1: Transform the string to avoid even/odd length issues
14+
let mut new_str = Vec::with_capacity(n * 2 + 1);
15+
new_str.push('#');
16+
for c in s.chars() {
17+
new_str.push(c);
18+
new_str.push('#');
19+
}
20+
21+
let m = new_str.len();
22+
let mut dp = vec![0; m];
23+
let mut friend_center = 0;
24+
let mut friend_radius = 0;
25+
let mut lps_center = 0;
26+
let mut lps_radius = 0;
27+
28+
// Step 2: Apply Manacher's Algorithm
29+
for i in 0..m {
30+
dp[i] = if friend_center + friend_radius > i {
31+
dp[2 * friend_center - i].min(friend_center + friend_radius - i)
32+
} else {
33+
1
34+
};
35+
36+
while i + dp[i] < m && i >= dp[i] && new_str[i + dp[i]] == new_str[i - dp[i]] {
37+
dp[i] += 1;
38+
}
39+
40+
if friend_center + friend_radius < i + dp[i] {
41+
friend_center = i;
42+
friend_radius = dp[i];
43+
}
44+
45+
if lps_radius < dp[i] {
46+
lps_center = i;
47+
lps_radius = dp[i];
48+
}
49+
}
50+
51+
// Step 3: Extract the longest palindromic substring
52+
let start = (lps_center - lps_radius + 1) / 2;
53+
let end = (lps_center + lps_radius - 1) / 2;
54+
s[start..end].to_string()
55+
}
56+
}

0 commit comments

Comments
 (0)