rust sort
leetcode 976
给定由一些正数(代表长度)组成的数组 A
,返回由其中三个长度组成的、面积不为零的三角形的最大周长。
如果不能形成任何面积不为零的三角形,返回 0
。
impl Solution { pub fn largest_perimeter(a: Vec<i32>) -> i32 { let mut a = a; a.sort_by(|a, b| b.cmp(&a)); for i in 0..a.len()-2 { if a[i] < a[i+1] + a [i+2] { return a[i] + a[i+1] + a[i+2]; } } 0 } }
sort_by源码
/// # Examples /// /// ``` /// let mut v = [5, 4, 1, 3, 2]; /// v.sort_by(|a, b| a.cmp(b)); /// assert!(v == [1, 2, 3, 4, 5]); /// /// // reverse sorting /// v.sort_by(|a, b| b.cmp(a)); /// assert!(v == [5, 4, 3, 2, 1]); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn sort_by<F>(&mut self, mut compare: F) where F: FnMut(&T, &T) -> Ordering { merge_sort(self, |a, b| compare(a, b) == Less); }
相关推荐
ztyzly00 2020-07-18
killgod 2020-06-14
小惠 2020-06-05
Oudasheng 2020-06-04
从零开始 2020-06-05
litterfrog 2020-05-30
老谢的自留地 2020-05-09
wulaxiaohei 2020-05-05
cyyking 2020-05-03
aaLiweipeng 2020-04-15
wordmhg 2020-04-09
wangqing 2020-04-06
Pinkr 2020-03-12
dushine00 2020-02-21
dbhllnr 2020-02-18
zhujiangtaotaise 2020-01-26
chenchuang 2020-01-25
troysps 2020-01-24