Skip to content

Commit 95be49f

Browse files
committed
704. Binary Search (Rust + Go improvements)
1 parent 04852d0 commit 95be49f

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-19%20%2F%202061-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-19%20%2F%202076-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
5+
![Language: Rust](https://img.shields.io/badge/language-Rust-00ADD8?logo=rust)
56

67
### LeetCode Algorithm
78

@@ -23,7 +24,7 @@
2324
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 |
2425
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string) | 🟢 |
2526
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Go](go/0404_sum_of_left_leaves) | 🟢 |
26-
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search) | 🟢 |
27+
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search), [Rust](rust/_0704_binary_search) | 🟢 |
2728

2829
### LeetCode Shell
2930

go/0704_binary_search/solution.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package binary_search
22

33
func Search(nums []int, target int) int {
4-
lowIdx, highIdx := 0, len(nums)-1
5-
for lowIdx <= highIdx {
6-
midIdx := (highIdx + lowIdx) / 2
7-
if nums[midIdx] == target {
8-
return midIdx
9-
}
4+
lowIdx, midIdx, highIdx := 0, 0, len(nums) - 1
105

11-
if nums[midIdx] < target {
12-
lowIdx = midIdx + 1
13-
} else {
14-
highIdx = midIdx - 1
15-
}
16-
}
6+
for lowIdx <= highIdx {
7+
midIdx = lowIdx + (highIdx - lowIdx) / 2
8+
if nums[midIdx] == target {
9+
return midIdx
10+
}
1711

18-
return -1
12+
if nums[midIdx] < target {
13+
lowIdx = midIdx + 1
14+
} else {
15+
highIdx = midIdx - 1
16+
}
17+
}
18+
19+
return -1
1920
}

go/0704_binary_search/solution_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func TestSearch(t *testing.T) {
1414
{nums: []int{-1, 0, 3, 5, 9, 12}, target: 9, want: 4},
1515
{nums: []int{-1, 0, 3, 5, 9, 12}, target: 2, want: -1},
1616
{nums: []int{-1, 0, 3, 5, 9, 12}, target: 3, want: 2},
17+
{nums: []int{5}, target: -5, want: -1},
18+
{nums: []int{}, target: 3, want: -1},
1719
}
1820

1921
for i, tc := range cases {

rust/_0704_binary_search/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "_0704_binary_search"
3+
version = "0.1.0"
4+
edition = "2021"

rust/_0704_binary_search/src/lib.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
2+
if nums.len() > 0 {
3+
let mut left = 0;
4+
let mut right = (nums.len() - 1) as i32;
5+
while left <= right {
6+
let mid = left + (right - left) / 2;
7+
let mid_idx = mid as usize;
8+
if nums[mid_idx] == target {
9+
return mid;
10+
}
11+
12+
if nums[mid_idx] < target {
13+
left = mid + 1;
14+
} else {
15+
right = mid - 1;
16+
}
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
#[test]
26+
fn search() {
27+
struct Case {
28+
nums: Vec<i32>,
29+
target: i32,
30+
want: i32,
31+
}
32+
33+
let cases = vec![
34+
Case { nums: vec![-1, 0, 3, 5, 9, 12], target: 9, want: 4 },
35+
Case { nums: vec![-1, 0, 3, 5, 9, 12], target: 2, want: -1 },
36+
Case { nums: vec![-1, 0, 3, 5, 9, 12], target: 3, want: 2 },
37+
Case { nums: vec![5], target: -5, want: -1 },
38+
Case { nums: vec![], target: 3, want: -1 },
39+
];
40+
41+
for case in cases.iter() {
42+
let got = super::search(case.nums.clone(), case.target);
43+
assert_eq!(case.want, got);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)