Skip to content

Commit 21d5c20

Browse files
committed
q35
1 parent d9eb2f0 commit 21d5c20

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/array/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod q35;

src/array/q35.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//! Given a sorted array of distinct integers and a target value, return the index if the target is found.
2+
//! If not, return the index where it would be if it were inserted in order.
3+
//!
4+
//! Example 1:
5+
//! ```
6+
//! Input: nums = [1,3,5,6], target = 5
7+
//! Output: 2
8+
//! ```
9+
//!
10+
//! Example 2:
11+
//! ```
12+
//! Input: nums = [1,3,5,6], target = 2
13+
//! Output: 1
14+
//! ```
15+
//!
16+
//! Example 3:
17+
//! ```
18+
//! Input: nums = [1,3,5,6], target = 7
19+
//! Output: 4
20+
//! ```
21+
//!
22+
//! Example 4:
23+
//! ```
24+
//! Input: nums = [1,3,5,6], target = 0
25+
//! Output: 0
26+
//! ```
27+
//!
28+
//! Example 5:
29+
//! ```
30+
//! Input: nums = [1], target = 0
31+
//! Output: 0
32+
//! ```
33+
//!
34+
//! Constraints:
35+
//! - 1 <= nums.length <= 104
36+
//! - -104 <= nums[i] <= 104
37+
//! - nums contains distinct values sorted in ascending order.
38+
//! - -104 <= target <= 104
39+
40+
41+
/// avg_time O(logn) space O(1)
42+
struct Solution ();
43+
impl Solution {
44+
45+
pub fn search_insert3(nums: Vec<i32>, target: i32) -> i32 {
46+
match nums.len() {
47+
0 => 0,
48+
v if &target >= nums.last().unwrap() => v as i32,
49+
v if &target <= nums.first().unwrap() => 0,
50+
v => {
51+
let mut left = 0;
52+
let mut right = v - 1;
53+
54+
while left <= right {
55+
let middle = left + (right - left ) /2;
56+
if nums[middle] > target {
57+
right = middle - 1;
58+
} else if nums[middle] < target {
59+
left = middle + 1
60+
} else {
61+
return middle as i32;
62+
}
63+
64+
}
65+
(right + 1) as i32
66+
}
67+
68+
}
69+
}
70+
71+
72+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
73+
match nums.len() {
74+
v if v == 0 || &target <= nums.first().unwrap() => 0,
75+
v if &target > nums.last().unwrap() => v as i32,
76+
v if &target == nums.last().unwrap() => (v - 1) as i32,
77+
v => {
78+
let mut left = 0;
79+
let mut right = v;
80+
81+
while left < right {
82+
let mid = left + (right - left) / 2;
83+
match nums[mid] {
84+
v if v > target => right = mid,
85+
v if v < target => left = mid+1,
86+
v => return mid as i32
87+
}
88+
}
89+
90+
right as i32
91+
}
92+
}
93+
}
94+
95+
}
96+
97+
98+
99+
#[cfg(test)]
100+
mod tests {
101+
use super::*;
102+
103+
#[test] fn common_test() {
104+
assert_eq!(Solution::search_insert(vec![1,3,5,6], 5), 2 );
105+
assert_eq!(Solution::search_insert(vec![1,3,5,6], -1), 0 );
106+
assert_eq!(Solution::search_insert(vec![1,3,5,6], 7), 4 );
107+
assert_eq!(Solution::search_insert(vec![1], 1), 0 );
108+
}
109+
}

0 commit comments

Comments
 (0)