From 625be9225d5b39329ff77bd6306391b3c1c47847 Mon Sep 17 00:00:00 2001 From: li-chx Date: Tue, 30 Sep 2025 10:27:27 +0800 Subject: [PATCH] =?UTF-8?q?2221.=20=E6=95=B0=E7=BB=84=E7=9A=84=E4=B8=89?= =?UTF-8?q?=E8=A7=92=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 458dfb5..9b2d341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,28 +2,17 @@ mod arr; struct Solution {} impl Solution { - pub fn triangle_number(nums: Vec) -> i32 { - if nums.len() < 3 { - return 0; - } - let mut nums = nums; - nums.sort(); - let mut ans = 0; - for i in 0..nums.len() - 2 { - let mut r = i + 2; - for l in i + 1..nums.len() - 1 { - while r < nums.len() && nums[i] + nums[l] > nums[r] { - r += 1; - } - - ans += ((r - l) as i32 - 1).max(0); + pub fn triangular_sum(mut nums: Vec) -> i32 { + for i in 0.. nums.len() - 1 { + for j in 0..nums.len() - 1 - i { + nums[j] = (nums[j] + nums[j + 1]) % 10; } } - ans + nums[0] } } fn main() { - let arr = vec![7, 0, 0, 0]; - println!("{}", Solution::triangle_number(arr)); + let arr = vec![1,2,3,4,5]; + println!("{}", Solution::triangular_sum(arr)); }