Skip to content

Commit

Permalink
To fix the sorted{ xxx } to sorted() & Add soapyigu flat-reduce solution
Browse files Browse the repository at this point in the history
Signed-off-by: Desgard_Duan <[email protected]>
  • Loading branch information
Desgard committed Aug 10, 2017
1 parent 4da412c commit e177010
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sort/ArrayPartitionI.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ArrayPartitionI {
func arrayPairSum(_ nums: [Int]) -> Int {
var arr = nums
arr = arr.sorted { $0 < $1 }
arr = arr.sorted()
var res = 0
for i in 0 ..< arr.count {
if i & 1 != 0 {
Expand All @@ -11,3 +11,11 @@ class ArrayPartitionI {
return res
}
}

class ArrayPartitionI_2 {
func arrayPairSum(_ nums: [Int]) -> Int {
return nums.sorted(by: <).enumerated()
.flatMap { $0 % 2 == 0 ? $1 : nil }
.reduce(0) { $0 + $1 }
}
}

0 comments on commit e177010

Please sign in to comment.