feat: binrary_search
This commit is contained in:
commit
9abef12a43
|
@ -0,0 +1,5 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="EMPTY_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="MaterialThemeProjectNewConfig">
|
||||||
|
<option name="metadata">
|
||||||
|
<MTProjectMetadataState>
|
||||||
|
<option name="migrated" value="true" />
|
||||||
|
<option name="pristineConfig" value="false" />
|
||||||
|
<option name="userId" value="6e282f31:18fd76255f2:-7fee" />
|
||||||
|
</MTProjectMetadataState>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/leetcode-rust.iml" filepath="$PROJECT_DIR$/.idea/leetcode-rust.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "binrary_search"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "binrary_search"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,49 @@
|
||||||
|
// 解題影片 https://www.youtube.com/watch?v=oxejtPgSlro&list=PL-qDGN2q6cbClYCRpJ2pyrlhYK9VjZmL3
|
||||||
|
// leetcode https://leetcode.com/problems/binary-search/
|
||||||
|
fn main() {
|
||||||
|
let case_a: Vec<i32> = vec![1, 2, 3];
|
||||||
|
let result = search(case_a, 3);
|
||||||
|
println!("Result: {}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 二元搜尋法 -> 左閉右閉原則
|
||||||
|
// 1. 先想停止條件
|
||||||
|
// 2. 想邊界值
|
||||||
|
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
|
||||||
|
// 先宣告需要的變數
|
||||||
|
let mut left: usize = 0;
|
||||||
|
let mut right: usize = nums.len();
|
||||||
|
|
||||||
|
// [1,2) 韓左邊不含右邊,試想如果 [1,1)這樣是合法區間嗎?,否所以是 <
|
||||||
|
while left < right {
|
||||||
|
// 防止溢位 |---------|-----------|
|
||||||
|
// 前 後
|
||||||
|
//=======================
|
||||||
|
// 中間 = 前 + (後 - 前)
|
||||||
|
// --------
|
||||||
|
// 2
|
||||||
|
//=======================
|
||||||
|
let mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
// 如果拿到了
|
||||||
|
if nums[mid] == target {
|
||||||
|
return mid as i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 左閉右開狀態
|
||||||
|
if nums[mid] > target {
|
||||||
|
if mid == 0 {
|
||||||
|
break;
|
||||||
|
} // 防止 mid - 1 溢出
|
||||||
|
// |---------|------|-----|
|
||||||
|
// mid. target
|
||||||
|
// 因為不包含,直接相等也不會算過
|
||||||
|
right = mid;
|
||||||
|
} else {
|
||||||
|
left = mid + 1;
|
||||||
|
// 因為包含在內,所以 mid 已經算過了,要讓他+1才是心區間
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc_fingerprint":13714326583451104423,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.78.0 (9b00956e5 2024-04-29)\nbinary: rustc\ncommit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6\ncommit-date: 2024-04-29\nhost: aarch64-apple-darwin\nrelease: 1.78.0\nLLVM version: 18.1.2\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/daniel/.rustup/toolchains/stable-aarch64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}}
|
|
@ -0,0 +1,3 @@
|
||||||
|
Signature: 8a477f597d28d172789f06886806bc55
|
||||||
|
# This file is a cache directory tag created by cargo.
|
||||||
|
# For information about cache directory tags see https://bford.info/cachedir/
|
|
@ -0,0 +1 @@
|
||||||
|
a6f852d213b481c9
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":792111255936306319,"features":"[]","declared_features":"","target":9999588162469818413,"profile":14453530908159220714,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/binrary_search-8833d8c297c074e6/dep-bin-binrary_search"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
This file has an mtime of when this was started.
|
Binary file not shown.
|
@ -0,0 +1 @@
|
||||||
|
/Users/daniel/Documents/Rust/leetcode-rust/binrary_search/target/debug/binrary_search: /Users/daniel/Documents/Rust/leetcode-rust/binrary_search/src/main.rs
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
||||||
|
/Users/daniel/Documents/Rust/leetcode-rust/binrary_search/target/debug/deps/binrary_search-8833d8c297c074e6: src/main.rs
|
||||||
|
|
||||||
|
/Users/daniel/Documents/Rust/leetcode-rust/binrary_search/target/debug/deps/binrary_search-8833d8c297c074e6.d: src/main.rs
|
||||||
|
|
||||||
|
src/main.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue