fix: gitignore
This commit is contained in:
parent
f9acaa43fe
commit
9db3ef0135
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "front_end_pointer_977"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "front_end_pointer_977"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,41 @@
|
||||||
|
// 解題影片 https://www.youtube.com/watch?v=9u1qXgHMb7Q&list=PL-qDGN2q6cbClYCRpJ2pyrlhYK9VjZmL3&index=3
|
||||||
|
// leetcode https://leetcode.com/problems/squares-of-a-sorted-array/
|
||||||
|
fn main() {
|
||||||
|
let case_a: Vec<i32> = vec![-4,-1,0,3,10];
|
||||||
|
let result = sorted_squares(case_a);
|
||||||
|
println!("Result: {:?}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input: nums = []
|
||||||
|
// Output: [0,1,9,16,100]
|
||||||
|
// Explanation: After squaring, the array becomes [16,1,0,9,100].
|
||||||
|
// After sorting, it becomes [0,1,9,16,100].
|
||||||
|
|
||||||
|
// 前後雙指針法
|
||||||
|
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
|
||||||
|
let mut left: usize = 0;
|
||||||
|
let mut right: usize = nums.len() - 1; // 陣列最後一個元素為長度 -1 因為起始為 0
|
||||||
|
let mut result: Vec<i32> = vec![0; nums.len()]; // 初始化結果向量
|
||||||
|
let mut new_pointer: isize = nums.len() as isize - 1; // 使用 isize 避免負索引
|
||||||
|
|
||||||
|
// 想停止條件,平方後的都做完了
|
||||||
|
while new_pointer >= 0 {
|
||||||
|
// 都是拿到比較大的,故從否面先塞
|
||||||
|
// 相等的話移動誰沒差,就移動前指針
|
||||||
|
if get_double(nums[left])>= get_double(nums[right]){
|
||||||
|
result[new_pointer as usize] = get_double(nums[left]);
|
||||||
|
new_pointer -= 1;
|
||||||
|
left += 1;
|
||||||
|
} else {
|
||||||
|
result[new_pointer as usize] = get_double(nums[right]);
|
||||||
|
new_pointer -= 1;
|
||||||
|
right -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_double(val: i32)->i32{
|
||||||
|
val * val
|
||||||
|
}
|
|
@ -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 @@
|
||||||
|
8c776d0a5cbcc935
|
|
@ -0,0 +1 @@
|
||||||
|
{"rustc":792111255936306319,"features":"[]","declared_features":"","target":2898340925340505798,"profile":14453530908159220714,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/front_end_pointer_977-f81ca00cf77d8b92/dep-bin-front_end_pointer_977"}}],"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.
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/3_front_end_pointer_977/target/debug/deps/front_end_pointer_977-f81ca00cf77d8b92: src/main.rs
|
||||||
|
|
||||||
|
/Users/daniel/Documents/Rust/leetcode-rust/array/3_front_end_pointer_977/target/debug/deps/front_end_pointer_977-f81ca00cf77d8b92.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/3_front_end_pointer_977/target/debug/front_end_pointer_977: /Users/daniel/Documents/Rust/leetcode-rust/array/3_front_end_pointer_977/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.
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