Skip to content

Commit 0ee5e39

Browse files
committed
binary search solition rust added
1 parent 9d00fec commit 0ee5e39

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| Python | [Python](Python/README.md) | [@muffafa](https://github.com/muffafa) | 14 |
66
| Java | [JAVA](Java/README.md) | [@cagridemirtash](https://github.com/cagridemirtash) | 3 |
77
| Javascript | [Javascript](Javascript/README.md) | [@kaanncavdar](https://github.com/kaanncavdar) | 4 |
8-
| Rust | [Rust](Rust/README.md) | [@sektor7k](https://github.com/sektor7k) | 12 |
8+
| Rust | [Rust](Rust/README.md) | [@sektor7k](https://github.com/sektor7k) | 13 |
99
| C++ | [Cpp](Cpp/README.md) | [@sametaydinq](https://github.com/sametaydinq) | 0 |
1010

1111
---

Rust/0704-binary-search/1-answer.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [✅ Counts the Frequencies to solve the 🧑🏻‍💻 Binary Search Problem on 🦀 Rust language]()
2+
3+
## 🧑🏻‍💻 Approach
4+
The function `search` takes in a vector of integers (`nums`) and a target integer (`target`).
5+
A copy of the `nums` vector is created and stored in `nums2` to avoid modifying the original vector.
6+
The `nums2` vector is sorted in ascending order using the `sort()` function.
7+
Pointers `low` and `high` are initialized to keep track of the search range in the sorted vector.
8+
The binary search algorithm is performed using a while loop, which continues until `low` is greater than `high`.
9+
Inside the loop, the middle index `mid` is calculated as the average of `low` and `high`.
10+
The element at the middle index (`nums2[mid]`) is compared to the target.
11+
If they are equal, it means the target has been found, and the index `mid` is returned as the result.
12+
If the element at the middle index is less than the target, it means the target is in the right half of the remaining range. Therefore, the `low` pointer is moved to `mid + 1`.
13+
If the element at the middle index is greater than the target, it means the target is in the left half of the remaining range. Therefore, the `high` pointer is moved to `mid - 1`.
14+
If the target is not found after the loop finishes, `-1` is returned as the result.
15+
16+
## 🔐 Code
17+
18+
``` rust
19+
impl Solution {
20+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
21+
let mut nums2 = nums.clone();
22+
nums2.sort();
23+
24+
let mut low = 0;
25+
let mut high = nums2.len() as i32 - 1;
26+
27+
while low <= high{
28+
let mid= (low+(high - low)/2) as usize;
29+
if nums2[mid] == target{
30+
return mid as i32;
31+
}
32+
else if nums2[mid]< target {
33+
low = mid as i32 +1;
34+
}
35+
else {
36+
high = mid as i32 -1
37+
}
38+
}
39+
-1
40+
}
41+
}
42+
```

Rust/0704-binary-search/question.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [Binary Search](https://leetcode.com/problems/binary-search/)
2+
3+
## 🚨 Problem
4+
<!-- Explanation of problem. -->
5+
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
6+
7+
You must write an algorithm with `O(log n)` runtime complexity.
8+
9+
**Example 1:**
10+
<!-- An example of problem. -->
11+
12+
>**Input:** nums = \[-1,0,3,5,9,12\], target = 9 </br> <!-- Input example. -->
13+
**Output:** 4 </br> <!-- Output example. -->
14+
**Explanation:** 9 exists in nums and its index is 4 <!-- Basic explanation of example. -->
15+
16+
**Example 2:**
17+
<!-- An example of problem. -->
18+
19+
>**Input:** nums = \[-1,0,3,5,9,12\], target = 2 </br> <!-- Input example. -->
20+
**Output:** -1 </br> <!-- Output example. -->
21+
**Explanation:** 2 does not exist in nums so return -1 <!-- Basic explanation of example. -->
22+
23+
**Constraints:**
24+
<!-- Constraints of problem. -->
25+
- $1 <= nums.length <= 10^4$
26+
- $-10^4 < nums[i], target < 10^4$
27+
- All the integers in `nums` are **unique**.
28+
- `nums` is sorted in ascending order.
29+
30+
## 🔐 Solutions
31+
<!-- Solutions of problem and their links. -->
32+
33+
| ID | METHOD |
34+
| :-- | :--------------------------: |
35+
| 1 | [Binary Search](1-answer.md) |

Rust/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| 10 | 0167 | [Two Sum II Input Array Is Sorted](0167-two-sum-2/question.md) | Two Pointers | Medium | Amazon |
1717
| 11 | 0121 | [Best Time to Buy and Sell Stock](0121-best-time-to-buy-and-sell-stock/question.md) | Sliding Window | Easy | |
1818
| 12 | 0206 | [Reverse Linked List](0206-reverse-linked-list/question.md) | Linked List | Easy |
19+
| 13 | 0704 | [Binary Search](0704-binary-search/question.md) | Binary Search | Easy |
1920

2021
<div align="center">
2122
<h1>Awesome Leetcode Algorithms Solututions With Rust</h1>

0 commit comments

Comments
 (0)