Skip to content

Commit

Permalink
leetcode: Add 0287
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 6, 2024
1 parent 5d040ed commit e628c33
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 41 deletions.
63 changes: 45 additions & 18 deletions src/leetcode/0035.search-insert-position/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,68 @@
use std::cmp::Ordering;

// Binary search
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
pub fn search_insert1(nums: Vec<i32>, target: i32) -> i32 {
assert!(!nums.is_empty());

let mut low = 0;
let mut high = nums.len() - 1;
while low + 1 < high {
let middle = low + (high - low) / 2;
if target < nums[0] {
return 0;
}
if target > nums[nums.len() - 1] {
return nums.len() as i32;
}

// 左闭右闭区间
let mut left: usize = 0;
let mut right: usize = nums.len() - 1;

// 终止循环的条件是 nums[left] > nums[right].
// 此时 left 所在位置就是 target 插入到数组中的位置.
while left <= right {
let middle = left + (right - left) / 2;
match nums[middle].cmp(&target) {
Ordering::Less => low = middle,
Ordering::Less => left = middle + 1,
Ordering::Equal => return middle as i32,
Ordering::Greater => high = middle,
Ordering::Greater => right = middle - 1,
}
}
if nums[high] < target {
high as i32 + 1
} else if nums[low] > target {
low as i32 - 1
} else {
high as i32
}

left as i32
}

fn main() {
pub type SolutionFn = fn(Vec<i32>, i32) -> i32;

fn check_solution(func: SolutionFn) {
let nums = vec![1, 3, 5, 6];
let target = 5;
let pos = search_insert(nums, target);
let pos = func(nums, target);
assert_eq!(pos, 2);

let nums = vec![1, 3, 5, 6];
let target = 2;
let pos = search_insert(nums, target);
let pos = func(nums, target);
assert_eq!(pos, 1);

let nums = vec![1, 3, 5, 6];
let target = 7;
let pos = search_insert(nums, target);
let pos = func(nums, target);
assert_eq!(pos, 4);

let nums = vec![1, 3, 5, 6];
let target = 0;
let pos = func(nums, target);
assert_eq!(pos, 0);
}

fn main() {
check_solution(search_insert1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, search_insert1};

#[test]
fn test_search_insert1() {
check_solution(search_insert1);
}
}
51 changes: 39 additions & 12 deletions src/leetcode/0069.sqrtx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

pub type SolutionFn = fn(i32) -> i32;
use std::cmp::Ordering;

// Binary Search
pub fn my_sqrt1(x: i32) -> i32 {
assert!(x >= 0);

pub fn my_sqrt(x: i32) -> i32 {
if x == 0 || x == 1 {
return x;
}

let mut left = 0;
let mut right = x;
let mut middle: i32;
let mut ans = 0;
let mut left: i32 = 0;
let mut right: i32 = x;
let mut ans: i32 = 0;
let x1 = x as i64;

while left <= right {
middle = left + (right - left) / 2;
let middle: i32 = left + (right - left) / 2;
let square = (middle as i64) * (middle as i64);
if square > x1 {
right = middle - 1;
Expand Down Expand Up @@ -46,10 +48,14 @@ pub fn my_sqrt2(x: i32) -> i32 {
}

pub fn my_sqrt3(x: i32) -> i32 {
use std::cmp::Ordering;
assert!(x >= 0);

let mut left = 1;
let mut right = x;
if x == 0 || x == 1 {
return x;
}

let mut left: i32 = 0;
let mut right: i32 = x;

while left <= right {
let middle = left + (right - left) / 2;
Expand All @@ -62,6 +68,8 @@ pub fn my_sqrt3(x: i32) -> i32 {
right
}

pub type SolutionFn = fn(i32) -> i32;

fn check_solution(func: SolutionFn) {
assert_eq!(func(36), 6);
assert_eq!(func(4), 2);
Expand All @@ -76,8 +84,27 @@ fn check_solution(func: SolutionFn) {
}

fn main() {
check_solution(my_sqrt);
check_solution(my_sqrt1);
check_solution(my_sqrt2);
check_solution(my_sqrt3);
println!("END");
}

#[cfg(test)]
mod tests {
use super::{check_solution, my_sqrt1, my_sqrt2, my_sqrt3};

#[test]
fn test_my_sqrt1() {
check_solution(my_sqrt1);
}

#[test]
fn test_my_sqrt2() {
check_solution(my_sqrt2);
}

#[test]
fn test_my_sqrt3() {
check_solution(my_sqrt3);
}
}
21 changes: 10 additions & 11 deletions src/leetcode/0137.single-number-ii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
// Use of this source is governed by General Public License that can be
// found in the LICENSE file.

use std::collections::HashMap;

// map, brute force
// 使用字典来统计数值出现的次数
pub fn single_number1(nums: Vec<i32>) -> i32 {
use std::collections::HashMap;
// 使用字典来统计数值出现的次数
let mut count = HashMap::<i32, i32>::new();
for num in &nums {
count
.entry(*num)
.and_modify(|value| *value += 1)
.or_insert(1);
let mut map: HashMap<i32, usize> = HashMap::new();
for &num in &nums {
map.entry(num).and_modify(|count| *count += 1).or_insert(1);
}
for (key, value) in count {
if value == 1 {
return key;
for (num, count) in map {
if count == 1 {
return num;
}
}
-1
Expand All @@ -37,6 +35,7 @@ pub fn single_number2(nums: Vec<i32>) -> i32 {
ans
}

// Bit Manipulation
pub fn single_number3(nums: Vec<i32>) -> i32 {
let mut ones = 0;
let mut twos = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/leetcode/0287.find-the-duplicate-number/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0287-find-the-duplicate-number"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0287.find-the-duplicate-number/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
Loading

0 comments on commit e628c33

Please sign in to comment.