Compare commits

..

2 Commits

Author SHA1 Message Date
daniel.w 6ea7008734 feature: mid-maximum-subarray 2024-07-16 13:52:00 +08:00
daniel.w 7b966bd4ba feature: mid-maximum-subarray 2024-07-16 13:51:32 +08:00
68 changed files with 119 additions and 3 deletions

View File

@ -1,8 +1,25 @@
fn main() {
println!("Hello, world!");
let mut case_a: Vec<i32> = vec![3, 2, 2, 3];
// 傳入時也要帖別聲明 &mut -> &mut case_a
let result = remove_duplicates(&mut case_a, 3);
println!("Result: {}", result);
}
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
let mut slow_pointer :usize= 0;
let mut fast_pointer:usize=0;
let mut fast_pointer:usize = 0;
let mut slow_pointer:usize = 0;
let stop = nums.len();
let mut target = nums[0];
// [), 想停止條件
while fast_pointer < stop {
if nums[fast_pointer] != target {
nums[slow_pointer] = nums[fast_pointer];
slow_pointer += 1;
target = nums[fast_pointer]
}
fast_pointer +=1;
}
return slow_pointer as i32
}

7
tg/maximum_subarray_53/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "maximum_subarray_53"
version = "0.1.0"

View File

@ -0,0 +1,6 @@
[package]
name = "maximum_subarray_53"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,43 @@
// 給訂一個陣列 array 拿出最大子集合的總和
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
if nums.len() <=0{
return 0;
}
let mut c_sum :i32 = nums[0]; // 目前的總和
let mut max_sum :i32 = nums[0]; // 總和的最大值
// 起始條件從一開始,原因是因為 0 已經為初始值,不用再算一次
for item in 1..nums.len() {
// 要跟自己比如果這個數字目前最大就從自己這個數字開始計算就好因為會有這個case 表示前面 c_sum + nums[item] 已經為負數
c_sum = max(nums[item], c_sum+nums[item]);
max_sum = max(max_sum,c_sum)
}
return max_sum;
}
pub fn max(a:i32, b:i32) -> i32 {
if a > b {
return a;
}
return b;
}
fn main() {
let case_a: Vec<i32> = vec![-2,1,-3,4,-1,2,1,-5,4];
let result = max_sub_array(case_a);
println!("Result: {}", result);
let case_b: Vec<i32> = vec![1];
let result = max_sub_array(case_b);
println!("Result: {}", result);
let case_c: Vec<i32> = vec![5,4,-1,7,8];
let result = max_sub_array(case_c);
println!("Result: {}", result);
}

View File

@ -0,0 +1,30 @@
Given an integer array nums, find the
subarray
with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

View File

@ -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":{}}

View File

@ -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/

View File

@ -0,0 +1 @@
{"rustc":792111255936306319,"features":"[]","declared_features":"","target":278490767354053006,"profile":14453530908159220714,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/maximum_subarray_53-c92090361541cf7f/dep-bin-maximum_subarray_53"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}

View File

@ -0,0 +1 @@
This file has an mtime of when this was started.

View File

@ -0,0 +1,5 @@
/Users/daniel/Documents/Rust/leetcode-rust/tg/maximum_subarray_53/target/debug/deps/maximum_subarray_53-c92090361541cf7f: src/main.rs
/Users/daniel/Documents/Rust/leetcode-rust/tg/maximum_subarray_53/target/debug/deps/maximum_subarray_53-c92090361541cf7f.d: src/main.rs
src/main.rs:

Binary file not shown.

View File

@ -0,0 +1 @@
/Users/daniel/Documents/Rust/leetcode-rust/tg/maximum_subarray_53/target/debug/maximum_subarray_53: /Users/daniel/Documents/Rust/leetcode-rust/tg/maximum_subarray_53/src/main.rs