博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
阅读量:5096 次
发布时间:2019-06-13

本文共 5712 字,大约阅读时间需要 19 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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 = 110         while evenPointer < A.count {11             12             if A[evenPointer] % 2 == 0 {13                 evenPointer += 214             } else {15                 A.swapAt(evenPointer, oddPointer)16                 oddPointer += 217             }18         }19         return A20     }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 += 210             }11             while (j < n && A[j] % 2 == 1) {12                 j += 213             }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 output20     }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..

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 copyA18     }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 result13     }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 == 010             {11                 ans[t] = x12                 t += 213             }         14         }15         t = 116         for x in A17         {18              if x % 2 == 119             {20                 ans[t] = x21                 t += 222             }23         }24         return ans25     }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 zipped15     }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{
//ji12 res2.append(num)13 } 14 }15 for i in 0..

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..

 

转载于:https://www.cnblogs.com/strengthen/p/9828530.html

你可能感兴趣的文章
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
Django中间件
查看>>
xcode 5.1安装vvdocument
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
MySQL更改默认的数据文档存储目录
查看>>
替代微软IIS强大的HTTP网站服务器工具
查看>>
6.5 案例21:将本地数据库中数据提交到服务器端
查看>>
PyQt5--EventSender
查看>>
android 通过AlarmManager实现守护进程
查看>>
Sql Server 中由数字转换为指定长度的字符串
查看>>
win7下把电脑设置成wlan热
查看>>