Skip to content

Commit baee6f0

Browse files
committed
add solution of binary_search
1 parent b531efc commit baee6f0

File tree

8 files changed

+64
-5
lines changed

8 files changed

+64
-5
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
nursery = { level = "warn", priority = -1 }
33
pedantic = { level = "warn", priority = -1 }
44
needless_pass_by_value = "allow"
5+
cast_possible_truncation = "allow"
6+
cast_possible_wrap = "allow"
7+
must_use_candidate = "allow"
58

69
[workspace]
710
members = [
11+
"crates/binary_search",
812
"crates/contains_duplicate",
913
"crates/is_palindrome",
1014
"crates/two_sum",

crates/binary_search/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "binary_search"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
8+
[lints]
9+
workspace = true

crates/binary_search/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::cmp::Ordering;
2+
3+
pub fn binary_search(nums: Vec<i32>, target: i32) -> i32 {
4+
let mut left_pointer = 0;
5+
let mut right_pointer = nums.len() - 1;
6+
7+
while left_pointer <= right_pointer {
8+
let middle = left_pointer + (right_pointer - left_pointer) / 2;
9+
10+
match target.cmp(&nums[middle]) {
11+
Ordering::Equal => return middle as i32,
12+
Ordering::Less => right_pointer = middle,
13+
Ordering::Greater => left_pointer = middle,
14+
}
15+
}
16+
17+
-1
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use super::*;
23+
24+
#[test]
25+
fn case_target_in_middle() {
26+
let result = binary_search(vec![1, 3, 5, 7, 9], 5);
27+
assert_eq!(result, 2);
28+
}
29+
30+
#[test]
31+
fn case_target_in_start() {
32+
let result = binary_search(vec![1, 3, 5, 7, 9, 11], 11);
33+
assert_eq!(result, 5);
34+
}
35+
36+
#[test]
37+
fn case_target_in_end() {
38+
let result = binary_search(vec![1, 3, 5, 7, 9], 9);
39+
assert_eq!(result, 4);
40+
}
41+
42+
#[test]
43+
fn case_not_found() {
44+
let result = binary_search(vec![1, 3, 5, 7, 9, 11], 0);
45+
assert_eq!(result, -1);
46+
}
47+
}

crates/contains_duplicate/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashSet;
22

3-
#[must_use]
43
pub fn contains_duplicate(numbers: Vec<i32>) -> bool {
54
let mut set = HashSet::new();
65

crates/is_palindrome/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#[must_use]
21
pub fn is_palindrome(s: String) -> bool {
32
if s.trim().is_empty() {
43
return true;

crates/two_sum/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use std::collections::HashMap;
22

3-
#[must_use]
43
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
54
let mut map = HashMap::new();
65

76
for (i, number) in nums.into_iter().enumerate() {
87
let diff = target - number;
98

109
if let Some(&j) = map.get(&diff) {
11-
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
1210
return vec![i as i32, j as i32];
1311
}
1412

crates/valid_anagram/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashMap;
22

3-
#[must_use]
43
pub fn is_anagram(s: String, t: String) -> bool {
54
if s.len() != t.len() {
65
return false;

0 commit comments

Comments
 (0)