为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II

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

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

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

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

Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.

You may return any answer array that satisfies this condition.

 Example 1:

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

 Note:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 0 <= A[i] <= 1000

给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。

对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。

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

 示例:

输入:[4,2,5,7]
输出:[4,5,2,7]
解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。

 提示:

  1. 2 <= A.length <= 20000
  2. A.length % 2 == 0
  3. 0 <= A[i] <= 1000

288ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         guard A.count > 1 else {
 4             return A
 5         }
 6         
 7         var A = A
 8         var evenPointer = 0
 9         var oddPointer = 1
10         while evenPointer < A.count {
11             
12             if A[evenPointer] % 2 == 0 {
13                 evenPointer += 2
14             } else {
15                 A.swapAt(evenPointer, oddPointer)
16                 oddPointer += 2
17             }
18         }
19         return A
20     }
21 }

292ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         let n = A.count
 4         var A = A
 5         var i = 0
 6         var j = 1
 7         while i < n && j < n {
 8             while i < n && A[i] % 2 == 0 {
 9                 i += 2
10             }
11             while (j < n && A[j] % 2 == 1) {
12                 j += 2
13             }
14             if i < n && j < n {
15                 (A[i], A[j]) = (A[j], A[i])
16             }
17         }
18         return A;
19     }
20 }

296ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         var outputEven: [Int] = []
 4         var outputOdd: [Int] = []
 5         
 6         for value in A {
 7             if value % 2 == 0 {
 8                 outputEven.append(value)
 9             } else {
10                 outputOdd.append(value)
11             }
12         }
13 
14         var output:[Int] = []
15         for index in 0 ..< outputEven.count {
16             output.append(outputEven[index])
17             output.append(outputOdd[index])
18         }
19         return output
20     }
21 }

300ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         
 4         var even = [Int]()
 5         var odd = [Int]()
 6         
 7         for i in A {
 8             if i % 2 == 0 {
 9                 even.append(i)
10             } else {
11                 odd.append(i)
12             }
13         }
14         
15         var output = [Int]()
16         
17         for i in 0..<even.count {
18             output.append(even[i])
19             output.append(odd[i])
20         }
21         
22         return output
23     }
24 }

308ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         var copyA = A
 4         var oddArray: [Int] = []
 5         var evenArray: [Int] = []
 6         for (index, interger) in A.enumerated() {
 7             if index % 2 == 0, interger % 2 != 0 {
 8                 oddArray.append(index)
 9             }
10             if index % 2 != 0, interger % 2 == 0 {
11                 evenArray.append(index)
12             }
13         }
14         for (index, _) in oddArray.enumerated() {
15             copyA.swapAt(oddArray[index], evenArray[index])
16         }
17         return copyA
18     }
19 }

320ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         var evenNums = A.filter{$0 % 2 != 0}
 4         var oddNums = A.filter{$0 % 2 == 0}
 5         var result = [Int]()
 6         
 7         for (index , _) in oddNums.enumerated() {
 8             result.append(oddNums[index])
 9             result.append(evenNums[index])
10         }
11         
12         return result
13     }
14 }

324ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         var N:Int = A.count
 4         var ans:[Int] = [Int](repeating: 0,count: N)
 5         
 6         var t:Int = 0
 7         for x in A
 8         {
 9             if x % 2 == 0
10             {
11                 ans[t] = x
12                 t += 2
13             }         
14         }
15         t = 1
16         for x in A
17         {
18              if x % 2 == 1
19             {
20                 ans[t] = x
21                 t += 2
22             }
23         }
24         return ans
25     }
26 }

328ms

 1 class Solution {
 2     func sortArrayByParityII(_ a: [Int]) -> [Int] {
 3         var evenNumbers: [Int] = []
 4         var oldNumbers: [Int] = []
 5         for n in a {
 6             if n % 2 == 0 {
 7                 evenNumbers.append(n)
 8             } else {
 9                 oldNumbers.append(n)
10             }
11         }
12         
13         let zipped = zip(evenNumbers,oldNumbers).flatMap{ [$0.0, $0.1] }
14         return zipped
15     }
16 }

368ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         var res1 = [Int]()
 4         var res2 = [Int]()
 5         var res3 = [Int]()
 6 
 7         for num in A {
 8             if num % 2 == 0{//ou
 9                 res1.append(num)
10             }
11             else{//ji
12                 res2.append(num)
13             } 
14         }
15         for i in 0..<res1.count{
16             res3.append(res1[i])
17             res3.append(res2[i])
18         }
19         return res3
20     }
21 }

416ms

 1 class Solution {
 2     func sortArrayByParityII(_ A: [Int]) -> [Int] {
 3         let b = A.sorted { (i, _) in return i % 2 == 0 }
 4         let count = A.count
 5         var result:[Int] = Array(repeating: 0, count: count)
 6         for i in 0..<count/2 {
 7             result[2*i] = b[i]
 8             result[2*i+1] = b[count-1-i]
 9         }
10 
11         return result
12     }
13 }

 

posted @ 2018-10-22 09:53  为敢技术  阅读(354)  评论(0编辑  收藏  举报