30 lines
657 B
Rust
30 lines
657 B
Rust
mod arr;
|
|
struct Solution {}
|
|
|
|
impl Solution {
|
|
pub fn triangle_number(nums: Vec<i32>) -> 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);
|
|
}
|
|
}
|
|
ans
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let arr = vec![7, 0, 0, 0];
|
|
println!("{}", Solution::triangle_number(arr));
|
|
}
|