元组解包

This commit is contained in:
游由 2021-11-16 16:27:43 +08:00
parent 2a59a85a35
commit 0d822fc959
4 changed files with 9 additions and 10 deletions

View File

@ -44,13 +44,12 @@ impl Solution {
pre = core::cmp::max(pre + x, x);
core::cmp::max(pre, acc)
})*/
/*nums.iter().fold((0, i32::MIN), |mut acc, &x| {
//(core::cmp::max(acc.0 + x, x), )
acc.0 = core::cmp::max(acc.0 + x, x);
acc.1 = core::cmp::max(acc.0, acc.1);
acc
}).1*/
nums.into_iter().fold((0, i32::MIN), |a, x| (x.max(a.0 + x), a.1.max(x.max(a.0 + x)))).1
nums.iter().fold((0, i32::MIN), |(mut a, mut b), &x| {
a = x.max(a + x);
b = b.max(a);
(a, b)
}).1
//nums.into_iter().fold((0, i32::MIN), |(a, b), x| (x.max(a + x), b.max(x.max(a + x)))).1
}
}

View File

@ -31,7 +31,7 @@ impl Solution {
///
/// 👍 1745 👎 0
pub fn climb_stairs(n: i32) -> i32 {
(1..n).fold((1, 2), |t, _| (t.1, t.0 + t.1)).0
(1..n).fold((1, 2), |(a, b), _| (b, a + b)).0
}
}

View File

@ -28,7 +28,7 @@ impl Solution {
///
/// 👍 1546 👎 0
pub fn rob(nums: Vec<i32>) -> i32 {
nums.into_iter().fold((0, 0), |t, x| (t.1, t.1.max(t.0 + x))).1
nums.into_iter().fold((0, 0), |(a, b), x| (b, b.max(a + x))).1
}
}

View File

@ -29,7 +29,7 @@ impl Solution {
///
/// 👍 588 👎 0
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
(2..=cost.len()).fold((0, 0), |t, i| (t.1, (t.1 + cost[i - 1]).min(t.0 + cost[i - 2]))).1
(2..=cost.len()).fold((0, 0), |(a,b), i| (b, (b + cost[i - 1]).min(a + cost[i - 2]))).1
}
}