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 3ee9481

Browse files
committed
add q35 q34
1 parent 91d5972 commit 3ee9481

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

‎array/Cargo.lock

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

‎array/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "array"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

‎array/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod q34;
2+
mod q704;
3+
mod q35;

‎array/src/q34.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::io::{self, Write};
21

32
struct Solution ();
43
impl Solution {

‎array/src/q35.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct Solution ();
2+
3+
impl Solution {
4+
5+
6+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
7+
8+
match nums.len() {
9+
v if v == 0 || nums.first().unwrap() >= &target => 0,
10+
v if nums.last().unwrap() == &target => (v - 1) as i32,
11+
v if nums.last().unwrap() < &target => v as i32,
12+
v => {
13+
let mut left = 0;
14+
let mut right = v - 1;
15+
16+
while left <= right {
17+
let mid = left + (right - left) / 2;
18+
match nums[mid] {
19+
m if m > target => right = mid - 1,
20+
m if m < target => left = mid + 1,
21+
_ => return mid as i32
22+
}
23+
}
24+
25+
(right+1) as i32
26+
}
27+
}
28+
29+
}
30+
31+
}
32+
33+
34+
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test] fn common_test() {
41+
assert_eq!(Solution::search_insert(vec![1,3,5,6], 5), 2 );
42+
assert_eq!(Solution::search_insert(vec![1,3,5,6], -1), 0 );
43+
assert_eq!(Solution::search_insert(vec![1,3,5,6], 7), 4 );
44+
assert_eq!(Solution::search_insert(vec![1,3,5,6], 2), 1 );
45+
assert_eq!(Solution::search_insert(vec![1,3], 3), 1 );
46+
assert_eq!(Solution::search_insert(vec![1], 1), 0 );
47+
}
48+
}

0 commit comments

Comments
(0)

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