• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode905.按奇偶排序数组|SortArrayByParity

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10608457.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

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:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。

你可以返回满足此条件的任何数组作为答案。 

示例:

输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。 

提示:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift线程安全数组发布时间:2022-07-13
下一篇:
[Swift]LeetCode1223.掷骰子模拟|DiceRollSimulation发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap