feat: binrary_search
This commit is contained in:
parent
9abef12a43
commit
61e416a0ba
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "fase_slwo_pointer_27"
|
||||
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "fase_slwo_pointer_27"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,48 @@
|
|||
fn main() {
|
||||
// 可變陣列要這樣用 rust
|
||||
let mut case_a: Vec<i32> = vec![3,2,2,3];
|
||||
// 傳入時也要帖別聲明 &mut -> &mut case_a
|
||||
let result = remove_element(&mut case_a, 3);
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
|
||||
// 移除元素,返回刪除後陣列長度,快慢指針,這樣做不需要移動將後面連續的空間每一個都移動 O(n) 解法
|
||||
// 1. 先想停止條件
|
||||
// 2. 想邊界值
|
||||
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
|
||||
let mut slow_pointer: usize = 0; // -> vec 長度從零開始
|
||||
let mut fast_pointer: usize = 0;
|
||||
|
||||
let stop = nums.len();
|
||||
// 先想停止條件
|
||||
while fast_pointer < stop {
|
||||
if nums[fast_pointer] != val {
|
||||
// 更新陣列中資料
|
||||
nums[slow_pointer] = nums[fast_pointer];
|
||||
slow_pointer = slow_pointer + 1;
|
||||
}
|
||||
fast_pointer = fast_pointer + 1;
|
||||
}
|
||||
|
||||
return slow_pointer as i32;
|
||||
}
|
||||
|
||||
// 口 口 口 口
|
||||
// 3,2,2,3
|
||||
// val = 3
|
||||
// ===============
|
||||
// f = 0 -> 第一個是我們不要的,所以慢指針不移動,數組不更新,快指針繼續移動
|
||||
// 3 口 口 口
|
||||
// s
|
||||
// ===============
|
||||
// f = 1 -> 是我們要的,1. 更新慢指針的位置元素為目前走訪到的元素 2. 更新慢指針位置,表示我們陣列已經更新到哪裡了,
|
||||
// 3 2 口 口 ---> 2 2 口 口
|
||||
// s ---> s
|
||||
// ===============
|
||||
// f = 2 -> 重複步驟
|
||||
// 2 2 口 口 ---> 2 2 2 口
|
||||
// s ---> s
|
||||
// ===============
|
||||
// f = 2 -> 重複步驟
|
||||
// 2 2 2 3 ---> 2 2 2 3
|
||||
// s ---> s
|
|
@ -0,0 +1 @@
|
|||
{"rustc_fingerprint":13714326583451104423,"outputs":{"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":""},"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":""}},"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 @@
|
|||
dd3a7bc4a9a96e30
|
|
@ -0,0 +1 @@
|
|||
{"rustc":792111255936306319,"features":"[]","declared_features":"","target":7613876442669931328,"profile":14453530908159220714,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/fase_slwo_pointer_27-a242af1aefb6edb9/dep-bin-fase_slwo_pointer_27"}}],"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.
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/array/fase_slwo_pointer_27/target/debug/deps/fase_slwo_pointer_27-a242af1aefb6edb9: src/main.rs
|
||||
|
||||
/Users/daniel/Documents/Rust/leetcode-rust/array/fase_slwo_pointer_27/target/debug/deps/fase_slwo_pointer_27-a242af1aefb6edb9.d: src/main.rs
|
||||
|
||||
src/main.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
/Users/daniel/Documents/Rust/leetcode-rust/array/fase_slwo_pointer_27/target/debug/fase_slwo_pointer_27: /Users/daniel/Documents/Rust/leetcode-rust/array/fase_slwo_pointer_27/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.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue