Skip to content

Commit e177010

Browse files
committed
To fix the sorted{ xxx } to sorted() & Add soapyigu flat-reduce solution
Signed-off-by: Desgard_Duan <gua@desgard.com>
1 parent 4da412c commit e177010

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Sort/ArrayPartitionI.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class ArrayPartitionI {
22
func arrayPairSum(_ nums: [Int]) -> Int {
33
var arr = nums
4-
arr = arr.sorted { $0 < $1 }
4+
arr = arr.sorted()
55
var res = 0
66
for i in 0 ..< arr.count {
77
if i & 1 != 0 {
@@ -11,3 +11,11 @@ class ArrayPartitionI {
1111
return res
1212
}
1313
}
14+
15+
class ArrayPartitionI_2 {
16+
func arrayPairSum(_ nums: [Int]) -> Int {
17+
return nums.sorted(by: <).enumerated()
18+
.flatMap { $0 % 2 == 0 ? $1 : nil }
19+
.reduce(0) { $0 + $1 }
20+
}
21+
}

0 commit comments

Comments
 (0)