Skip to content

Commit 3edd36e

Browse files
committed
Added rust
1 parent eade80a commit 3edd36e

File tree

101 files changed

+8146
-410
lines changed

Some content is hidden

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

101 files changed

+8146
-410
lines changed

README.md

Lines changed: 410 additions & 410 deletions
Large diffs are not rendered by default.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 1\. Two Sum
5+
6+
Easy
7+
8+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9+
10+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
11+
12+
You can return the answer in any order.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,7,11,15], target = 9
17+
18+
**Output:** [0,1]
19+
20+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,2,4], target = 6
25+
26+
**Output:** [1,2]
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [3,3], target = 6
31+
32+
**Output:** [0,1]
33+
34+
**Constraints:**
35+
36+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
39+
* **Only one valid answer exists.**
40+
41+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
42+
43+
## Solution
44+
45+
```rust
46+
use std::collections::HashMap;
47+
48+
impl Solution {
49+
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
50+
let mut index_map: HashMap<i32, usize> = HashMap::new();
51+
52+
for (i, &num) in numbers.iter().enumerate() {
53+
let required_num = target - num;
54+
if let Some(&index) = index_map.get(&required_num) {
55+
return vec![index as i32, i as i32];
56+
}
57+
index_map.insert(num, i);
58+
}
59+
60+
vec![-1, -1]
61+
}
62+
}
63+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 2\. Add Two Numbers
5+
6+
Medium
7+
8+
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.
9+
10+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
15+
16+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
17+
18+
**Output:** [7,0,8]
19+
20+
**Explanation:** 342 + 465 = 807.
21+
22+
**Example 2:**
23+
24+
**Input:** l1 = [0], l2 = [0]
25+
26+
**Output:** [0]
27+
28+
**Example 3:**
29+
30+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
31+
32+
**Output:** [8,9,9,9,0,0,0,1]
33+
34+
**Constraints:**
35+
36+
* The number of nodes in each linked list is in the range `[1, 100]`.
37+
* `0 <= Node.val <= 9`
38+
* It is guaranteed that the list represents a number that does not have leading zeros.
39+
40+
## Solution
41+
42+
```rust
43+
// Definition for singly-linked list.
44+
// pub struct ListNode {
45+
// pub val: i32,
46+
// pub next: Option<Box<ListNode>>
47+
// }
48+
//
49+
// impl ListNode {
50+
// #[inline]
51+
// fn new(val: i32) -> Self {
52+
// ListNode {
53+
// next: None,
54+
// val
55+
// }
56+
// }
57+
// }
58+
impl Solution {
59+
pub fn add_two_numbers(
60+
l1: Option<Box<ListNode>>,
61+
l2: Option<Box<ListNode>>
62+
) -> Option<Box<ListNode>> {
63+
let mut p = l1;
64+
let mut q = l2;
65+
let mut dummy_head = Box::new(ListNode::new(0));
66+
let mut curr = &mut dummy_head;
67+
let mut carry = 0;
68+
69+
while p.is_some() || q.is_some() {
70+
let x = p.as_ref().map_or(0, |node| node.val);
71+
let y = q.as_ref().map_or(0, |node| node.val);
72+
let sum = carry + x + y;
73+
carry = sum / 10;
74+
curr.next = Some(Box::new(ListNode::new(sum % 10)));
75+
curr = curr.next.as_mut().unwrap();
76+
77+
if let Some(node) = p {
78+
p = node.next;
79+
}
80+
if let Some(node) = q {
81+
q = node.next;
82+
}
83+
}
84+
85+
if carry > 0 {
86+
curr.next = Some(Box::new(ListNode::new(carry)));
87+
}
88+
89+
dummy_head.next
90+
}
91+
}
92+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 3\. Longest Substring Without Repeating Characters
5+
6+
Medium
7+
8+
Given a string `s`, find the length of the **longest substring** without repeating characters.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "abcabcbb"
13+
14+
**Output:** 3
15+
16+
**Explanation:** The answer is "abc", with the length of 3.
17+
18+
**Example 2:**
19+
20+
**Input:** s = "bbbbb"
21+
22+
**Output:** 1
23+
24+
**Explanation:** The answer is "b", with the length of 1.
25+
26+
**Example 3:**
27+
28+
**Input:** s = "pwwkew"
29+
30+
**Output:** 3
31+
32+
**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.
33+
34+
**Example 4:**
35+
36+
**Input:** s = ""
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
43+
* `s` consists of English letters, digits, symbols and spaces.
44+
45+
## Solution
46+
47+
```rust
48+
impl Solution {
49+
pub fn length_of_longest_substring(s: String) -> i32 {
50+
// Array to store last seen indices of characters
51+
let mut last_indices = [-1; 256];
52+
let mut max_len = 0;
53+
let mut cur_len = 0;
54+
let mut start = 0;
55+
56+
// Convert string to bytes to use indexing
57+
let bytes = s.as_bytes();
58+
for (i, &cur) in bytes.iter().enumerate() {
59+
// Cast byte to usize to use as an index
60+
let cur = cur as usize;
61+
62+
if last_indices[cur] < start as i32 {
63+
last_indices[cur] = i as i32;
64+
cur_len += 1;
65+
} else {
66+
let last_index = last_indices[cur];
67+
start = (last_index + 1) as usize;
68+
cur_len = i - start + 1;
69+
last_indices[cur] = i as i32;
70+
}
71+
72+
if cur_len > max_len {
73+
max_len = cur_len;
74+
}
75+
}
76+
77+
max_len as i32
78+
}
79+
}
80+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 4\. Median of Two Sorted Arrays
5+
6+
Hard
7+
8+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
9+
10+
The overall run time complexity should be `O(log (m+n))`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums1 = [1,3], nums2 = [2]
15+
16+
**Output:** 2.00000
17+
18+
**Explanation:** merged array = [1,2,3] and median is 2.
19+
20+
**Example 2:**
21+
22+
**Input:** nums1 = [1,2], nums2 = [3,4]
23+
24+
**Output:** 2.50000
25+
26+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
27+
28+
**Example 3:**
29+
30+
**Input:** nums1 = [0,0], nums2 = [0,0]
31+
32+
**Output:** 0.00000
33+
34+
**Example 4:**
35+
36+
**Input:** nums1 = [], nums2 = [1]
37+
38+
**Output:** 1.00000
39+
40+
**Example 5:**
41+
42+
**Input:** nums1 = [2], nums2 = []
43+
44+
**Output:** 2.00000
45+
46+
**Constraints:**
47+
48+
* `nums1.length == m`
49+
* `nums2.length == n`
50+
* `0 <= m <= 1000`
51+
* `0 <= n <= 1000`
52+
* `1 <= m + n <= 2000`
53+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
54+
55+
## Solution
56+
57+
```rust
58+
impl Solution {
59+
pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
60+
let (nums1, nums2) = if nums1.len() > nums2.len() {
61+
// Ensures nums1 is the smaller array
62+
(nums2, nums1)
63+
} else {
64+
(nums1, nums2)
65+
};
66+
67+
let n1 = nums1.len();
68+
let n2 = nums2.len();
69+
let mut low = 0;
70+
let mut high = n1;
71+
72+
while low <= high {
73+
let cut1 = (low + high) / 2;
74+
let cut2 = (n1 + n2 + 1) / 2 - cut1;
75+
76+
let l1 = if cut1 == 0 { i32::MIN } else { nums1[cut1 - 1] };
77+
let l2 = if cut2 == 0 { i32::MIN } else { nums2[cut2 - 1] };
78+
let r1 = if cut1 == n1 { i32::MAX } else { nums1[cut1] };
79+
let r2 = if cut2 == n2 { i32::MAX } else { nums2[cut2] };
80+
81+
if l1 <= r2 && l2 <= r1 {
82+
// Found the correct partition
83+
if (n1 + n2) % 2 == 0 {
84+
return (f64::from(l1.max(l2)) + f64::from(r1.min(r2))) / 2.0;
85+
} else {
86+
return f64::from(l1.max(l2));
87+
}
88+
} else if l1 > r2 {
89+
high = cut1 - 1;
90+
} else {
91+
low = cut1 + 1;
92+
}
93+
}
94+
95+
// Fallback case, should never hit
96+
0.0
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)