在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an array You may return any answer array that satisfies this condition. Example 1: Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Note:
给定一个非负整数数组 你可以返回满足此条件的任何数组作为答案。 示例: 输入:[3,1,2,4] 输出:[2,4,3,1] 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。 提示:
68ms
1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 guard !A.isEmpty else { 4 return [] 5 } 6 var array = A 7 var left = 0 8 var right = A.count - 1 9 while(left < right && right > left) { 10 while(array[left] & 1 == 0 && left < right) { 11 left += 1 12 } 13 while(array[right] & 1 == 1 && right > left) { 14 right -= 1 15 } 16 if left != right { 17 let temp = array[left] 18 array[left] = array[right] 19 array[right] = temp 20 left += 1 21 right -= 1 22 } 23 } 24 return array 25 } 26 } Runtime: 72 ms
Memory Usage: 19.4 MB
1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var A = A 4 var left = 0 5 var right = 0 6 let count = A.count 7 while right < count { 8 // 偶数 9 if A[right] & 1 == 0 { 10 if left != right { 11 A[left] ^= A[right] 12 A[right] = A[left] ^ A[right] 13 A[left] ^= A[right] 14 } 15 left += 1 16 } 17 right += 1 18 } 19 return A 20 } 21 } 72ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var even: [Int] = [], odd: [Int] = [] 4 A.forEach { 5 if $0 % 2 == 0 { 6 even.append($0) 7 } else { 8 odd.append($0) 9 } 10 } 11 12 return even + odd 13 } 14 } 76ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var res = A 4 var i = 0, pos = 0 5 6 while i < A.count { 7 if res[i] & 1 == 0 { 8 let val = res.remove(at: i) 9 res.insert(val, at: pos) 10 pos += 1 11 } 12 i += 1 13 } 14 15 return res 16 } 17 } 80ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var parity: [Int] = [] 4 5 for num in A { 6 if num & 0b1 == 0b1 { 7 parity.insert(num, at: parity.count) 8 } else { 9 parity.insert(num, at: 0) 10 } 11 } 12 13 return parity 14 } 15 } 84ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var arr = A 4 var i = 0 5 var j = A.count - 1 6 while (i < j) { 7 if (arr[i] % 2 > arr[j] % 2) { 8 var temp = arr[i] 9 arr[i] = arr[j] 10 arr[j] = temp 11 } 12 13 if (arr[i] % 2 == 0) { i += 1 } 14 if (arr[j] % 2 == 1) { j -= 1 } 15 } 16 return arr 17 } 18 } 88ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 let odd = A.filter { (num) -> Bool in 4 return (num % 2 == 1) 5 } 6 let even = A.filter { (num) -> Bool in 7 return (num % 2 == 0) 8 } 9 return even + odd 10 } 11 } 100ms class Solution { func sortArrayByParity(_ A: [Int]) -> [Int] { return A.sorted(by: { $0 % 2 < $1 % 2 }) } } 104ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 var B = A 4 _ = B.partition(by: { $0 % 2 != 0 }) 5 return B 6 } 7 } 116ms class Solution { func sortArrayByParity(_ A: [Int]) -> [Int] { return A.sorted(by: { return $0 % 2 == 0 && $1 % 2 == 1}) } } 132ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 return A.sorted(by: { (lhs, rhs) -> Bool in 4 return lhs % 2 == 0 5 }) 6 } 7 } 184ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 return A.sorted { lhs, rhs in 4 switch (lhs % 2, rhs % 2) { 5 case (0, 0): 6 return true 7 case (0, 1): 8 return true 9 case (1, 0): 10 return false 11 case (1, 1): 12 return true 13 default: 14 fatalError() 15 } 16 } 17 } 18 } 248ms 1 class Solution { 2 func sortArrayByParity(_ A: [Int]) -> [Int] { 3 return A.filter{ $0 % 2 == 0 } + A.filter { $0 % 2 != 0 } 4 } 5 }
|
请发表评论