Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e95284

Browse files
committed
Add solutions
1 parent ef0f53d commit 4e95284

File tree

5 files changed

+153
-1
lines changed

5 files changed

+153
-1
lines changed

‎README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
LeetCode is a website that has programming-related questions that are designed to be solved in a limited amount of time. This repository is a collection of some of my solutions written in [Rust](https://www.rust-lang.org/).
88

9-
## Solutions (100)
9+
## Solutions (105)
1010
| No. | Title | Solution | Problem | Difficulty |
1111
|:---:|:------|:--------:|:-------:|:----------:|
1212
| 1 | Two Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/two_sum.rs) | [Leetcode](https://leetcode.com/problems/two-sum/) | Easy |
@@ -42,6 +42,7 @@ LeetCode is a website that has programming-related questions that are designed t
4242
| 212 | Word Search II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_words.rs) | [Leetcode](https://leetcode.com/problems/word-search-ii/) | Hard |
4343
| 230 | Kth Smallest Element in a BST | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/kth_smallest.rs) | [Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | Medium |
4444
| 242 | Valid Anagram | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_anagram.rs) | [Leetcode](https://leetcode.com/problems/valid-anagram/) | Easy |
45+
| 253 | Meeting Rooms II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_meeting_rooms.rs) | [Leetcode](https://leetcode.com/problems/meeting-rooms-ii/) | Medium |
4546
| 282 | Expression Add Operators | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/add_operators.rs) | [Leetcode](https://leetcode.com/problems/expression-add-operators/) | Hard |
4647
| 317 | Shortest Distance from All Buildings | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_distance.rs) | [Leetcode](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | Hard |
4748
| 322 | Coin Change | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/coin_change.rs) | [Leetcode](https://leetcode.com/problems/coin-change/) | Medium |
@@ -50,6 +51,7 @@ LeetCode is a website that has programming-related questions that are designed t
5051
| 337 | House Robber III | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/rob.rs) | [Leetcode](https://leetcode.com/problems/house-robber-iii/) | Medium |
5152
| 350 | Intersection of Two Arrays II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/intersect.rs) | [Leetcode](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | Easy |
5253
| 368 | Largest Divisible Subset | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/largest_divisible_subset.rs) | [Leetcode](https://leetcode.com/problems/largest-divisible-subset/) | Medium |
54+
| 374 | Guess Number Higher or Lower | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/guess_number.rs) | [Leetcode](https://leetcode.com/problems/guess-number-higher-or-lower/) | Easy |
5355
| 387 | First Unique Character in a String | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/first_uniq_char.rs) | [Leetcode](https://leetcode.com/problems/first-unique-character-in-a-string/) | Easy |
5456
| 435 | Non-overlapping Intervals | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/erase_overlap_intervals.rs) | [Leetcode](https://leetcode.com/problems/non-overlapping-intervals/) | Medium |
5557
| 442 | Find All Duplicates in an Array | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_duplicates.rs) | [Leetcode](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | Medium |
@@ -60,7 +62,9 @@ LeetCode is a website that has programming-related questions that are designed t
6062
| 516 | Longest Palindromic Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/longest_palindrome_subseq.rs) | [Leetcode](https://leetcode.com/problems/longest-palindromic-subsequence/) | Medium |
6163
| 543 | Diameter of Binary Tree | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/diameter_of_binary_tree.rs) | [Leetcode](https://leetcode.com/problems/diameter-of-binary-tree/) | Easy |
6264
| 547 | Number of Provinces | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/find_circle_num.rs) | [Leetcode](https://leetcode.com/problems/number-of-provinces/) | Medium |
65+
| 696 | Count Binary Substrings | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/count_binary_substrings.rs) | [Leetcode](https://leetcode.com/problems/count-binary-substrings/) | Easy |
6366
| 698 | Partition to K Equal Sum Subsets | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/can_partition_k_subsets.rs) | [Leetcode](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) | Medium |
67+
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/cheat.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
6468
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
6569
| 725 | Split Linked List in Parts | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/split_list_to_parts.rs) | [Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/) | Medium |
6670
| 741 | Cherry Pickup | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/cherry_pickup.rs) | [Leetcode](https://leetcode.com/problems/cherry-pickup/) | Hard |
@@ -74,6 +78,7 @@ LeetCode is a website that has programming-related questions that are designed t
7478
| 937 | Reorder Data in Log Files | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/reorder_log_files.rs) | [Leetcode](https://leetcode.com/problems/reorder-data-in-log-files/) | Easy |
7579
| 973 | K Closest Points to Origin | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/k_closest.rs) | [Leetcode](https://leetcode.com/problems/k-closest-points-to-origin/) | Medium |
7680
| 991 | Broken Calculator | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/broken_calc.rs) | [Leetcode](https://leetcode.com/problems/broken-calculator/) | Medium |
81+
| 1008 | Construct Binary Search Tree from Preorder Traversal | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/bst_from_preorder.rs) | [Leetcode](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/) | Medium |
7782
| 1013 | Partition Array Into Three Parts With Equal Sum | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/can_three_parts_equal_sum.rs) | [Leetcode](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | Easy |
7883
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
7984
| 1041 | Robot Bounded In Circle | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/interview/amazon/is_robot_bounded.rs) | [Leetcode](https://leetcode.com/problems/robot-bounded-in-circle/submissions/) | Medium |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Definition for a binary tree node.
2+
#[derive(Debug, PartialEq, Eq)]
3+
pub struct TreeNode {
4+
pub val: i32,
5+
pub left: Option<Rc<RefCell<TreeNode>>>,
6+
pub right: Option<Rc<RefCell<TreeNode>>>,
7+
}
8+
9+
impl TreeNode {
10+
#[inline]
11+
pub fn new(val: i32) -> Self {
12+
TreeNode { val, left: None, right: None }
13+
}
14+
}
15+
16+
use std::cell::RefCell;
17+
use std::rc::Rc;
18+
19+
// 1008. Construct Binary Search Tree from Preorder Traversal, Medium
20+
// https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
21+
impl Solution {
22+
pub fn bst_from_preorder(preorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
23+
fn helper(i: &mut usize, preorder: &Vec<i32>, lower: i32, upper: i32) -> Option<Rc<RefCell<TreeNode>>> {
24+
let n = preorder.len();
25+
26+
if *i >= n {
27+
return None;
28+
}
29+
30+
let val = preorder[*i];
31+
if val < lower || val > upper {
32+
return None;
33+
}
34+
35+
*i += 1;
36+
37+
let root = Rc::new(RefCell::new(TreeNode::new(val)));
38+
root.borrow_mut().left = helper(i, &preorder, lower, val);
39+
root.borrow_mut().right = helper(i, &preorder, val, upper);
40+
41+
return Some(root);
42+
}
43+
44+
let mut i = 0;
45+
return helper(&mut i, &preorder, i32::MIN, i32::MAX);
46+
}
47+
}
48+
49+
struct Solution {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 696. Count Binary Substrings, Easy
2+
// https://leetcode.com/problems/count-binary-substrings/
3+
impl Solution {
4+
pub fn count_binary_substrings(s: String) -> i32 {
5+
let mut chars = s.chars().collect::<Vec<char>>();
6+
chars.push('#');
7+
let n = s.len();
8+
if n == 0 {
9+
return 0;
10+
}
11+
12+
let mut prev = chars[0];
13+
let mut count = 1;
14+
15+
let mut counts = vec![];
16+
for i in 1..n {
17+
if prev == chars[i] {
18+
count += 1;
19+
} else {
20+
counts.push(count);
21+
prev = chars[i];
22+
count = 1;
23+
}
24+
}
25+
26+
let mut substrings_count = 0;
27+
for i in 0..counts.len() - 1 {
28+
substrings_count += i32::min(counts[i], counts[i - 1]);
29+
}
30+
31+
substrings_count
32+
}
33+
}
34+
35+
struct Solution {}

‎src/leetcode/problem/guess_number.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 374. Guess Number Higher or Lower, Easy
2+
// https://leetcode.com/problems/guess-number-higher-or-lower/
3+
impl Solution {
4+
unsafe fn guess(n: i32) -> i32 {
5+
1
6+
}
7+
8+
unsafe fn guessNumber(n: i32) -> i32 {
9+
let mut l = 0;
10+
let mut r = n;
11+
12+
while l < r {
13+
let mid = l + (r - l) / 2;
14+
15+
match Solution::guess(mid) {
16+
-1 => {
17+
r = mid - 1;
18+
}
19+
1 => {
20+
l = mid + 1;
21+
}
22+
0 => {
23+
return mid;
24+
}
25+
_ => unreachable!(),
26+
}
27+
}
28+
29+
l
30+
}
31+
}
32+
33+
struct Solution {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::collections::BinaryHeap;
2+
3+
// 253. Meeting Rooms II, Medium
4+
// https://leetcode.com/problems/meeting-rooms-ii/
5+
impl Solution {
6+
pub fn min_meeting_rooms(intervals: Vec<Vec<i32>>) -> i32 {
7+
let mut intervals = intervals;
8+
intervals.sort_unstable_by(|a, b| a[0].cmp(&b[0]));
9+
10+
let mut rooms = 0;
11+
let mut heap = BinaryHeap::new();
12+
13+
for interval in intervals.iter() {
14+
if heap.is_empty() {
15+
heap.push(interval[1]);
16+
rooms += 1;
17+
} else if heap.peek().unwrap() <= &interval[0] {
18+
heap.pop();
19+
heap.push(interval[1]);
20+
} else {
21+
heap.push(interval[1]);
22+
rooms += 1;
23+
}
24+
}
25+
26+
rooms
27+
}
28+
}
29+
30+
struct Solution {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /