數據結構與算法之按奇偶排序數組II
一道簡單模擬題,來做做看!
按奇偶排序數組II
力扣題目鏈接:https://leetcode-cn.com/problems/sort-array-by-parity-ii/
給定一個非負整數數組 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] 也會被接受。
思路
這道題目直接的想法可能是兩層for循環再加上used數組表示使用過的元素。這樣的的時間復雜度是O(n^2)。
方法一
其實這道題可以用很樸實的方法,時間復雜度就就是O(n)了,C++代碼如下:
- class Solution {
- public:
- vector<int> sortArrayByParityII(vector<int>& A) {
- vector<int> even(A.size() / 2); // 初始化就確定數組大小,節省開銷
- vector<int> odd(A.size() / 2);
- vector<int> result(A.size());
- int evenIndex = 0;
- int oddIndex = 0;
- int resultIndex = 0;
- // 把A數組放進偶數數組,和奇數數組
- for (int i = 0; i < A.size(); i++) {
- if (A[i] % 2 == 0) even[evenIndex++] = A[i];
- else odd[oddIndex++] = A[i];
- }
- // 把偶數數組,奇數數組分別放進result數組中
- for (int i = 0; i < evenIndex; i++) {
- result[resultIndex++] = even[i];
- result[resultIndex++] = odd[i];
- }
- return result;
- }
- };
- 時間復雜度:O(n)
- 空間復雜度:O(n)
方法二
以上代碼我是建了兩個輔助數組,而且A數組還相當于遍歷了兩次,用輔助數組的好處就是思路清晰,優化一下就是不用這兩個輔助樹,代碼如下:
- class Solution {
- public:
- vector<int> sortArrayByParityII(vector<int>& A) {
- vector<int> result(A.size());
- int evenIndex = 0; // 偶數下表
- int oddIndex = 1; // 奇數下表
- for (int i = 0; i < A.size(); i++) {
- if (A[i] % 2 == 0) {
- result[evenIndex] = A[i];
- evenIndex += 2;
- }
- else {
- result[oddIndex] = A[i];
- oddIndex += 2;
- }
- }
- return result;
- }
- };
- 時間復雜度O(n)
- 空間復雜度O(n)
方法三
當然還可以在原數組上修改,連result數組都不用了。
- class Solution {
- public:
- vector<int> sortArrayByParityII(vector<int>& A) {
- int oddIndex = 1;
- for (int i = 0; i < A.size(); i += 2) {
- if (A[i] % 2 == 1) { // 在偶數位遇到了奇數
- while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇數位找一個偶數
- swap(A[i], A[oddIndex]); // 替換
- }
- }
- return A;
- }
- };
- 時間復雜度:O(n)
- 空間復雜度:O(1)
這里時間復雜度并不是O(n^2),因為偶數位和奇數位都只操作一次,不是n/2 * n/2的關系,而是n/2 + n/2的關系!
其他語言版本
Java
- // 方法一
- class Solution {
- public int[] sortArrayByParityII(int[] nums) {
- // 分別存放 nums 中的奇數、偶數
- int len = nums.length;
- int evenIndex = 0;
- int oddIndex = 0;
- int[] even = new int[len / 2];
- int[] odd = new int[len / 2];
- for (int i = 0; i < len; i++) {
- if (nums[i] % 2 == 0) {
- even[evenIndex++] = nums[i];
- } else {
- odd[oddIndex++] = nums[i];
- }
- }
- // 把奇偶數組重新存回 nums
- int index = 0;
- for (int i = 0; i < even.length; i++) {
- nums[index++] = even[i];
- nums[index++] = odd[i];
- }
- return nums;
- }
- }
Python3
- #方法2
- class Solution:
- def sortArrayByParityII(self, nums: List[int]) -> List[int]:
- result = [0]*len(nums)
- evenIndex = 0
- oddIndex = 1
- for i in range(len(nums)):
- if nums[i] % 2: #奇數
- result[oddIndex] = nums[i]
- oddIndex += 2
- else: #偶數
- result[evenIndex] = nums[i]
- evenIndex += 2
- return result
- #方法3
- class Solution:
- def sortArrayByParityII(self, nums: List[int]) -> List[int]:
- oddIndex = 1
- for i in range(0,len(nums),2): #步長為2
- if nums[i] % 2: #偶數位遇到奇數
- while nums[oddIndex] % 2: #奇數位找偶數
- oddIndex += 2
- nums[i], nums[oddIndex] = nums[oddIndex], nums[i]
- return nums
Go
- // 方法一
- func sortArrayByParityII(nums []int) []int {
- // 分別存放 nums 中的奇數、偶數
- even, odd := []int{}, []int{}
- for i := 0; i < len(nums); i++ {
- if (nums[i] % 2 == 0) {
- even = append(even, nums[i])
- } else {
- odd = append(odd, nums[i])
- }
- }
- // 把奇偶數組重新存回 nums
- result := make([]int, len(nums))
- index := 0
- for i := 0; i < len(even); i++ {
- result[index] = even[i]; index++;
- result[index] = odd[i]; index++;
- }
- return result;
- }
JavaScript
- //方法一
- var sortArrayByParityII = function(nums) {
- const n = nums.length;
- // 分別存放 nums 中的奇數、偶數
- let evenIndex = 0, oddIndex = 0;
- // 初始化就確定數組大小,節省開銷
- const even = new Array(Math.floor(n/2));
- const odd = new Array(Math.floor(n/2));
- // 把A數組放進偶數數組,和奇數數組
- for(let i = 0; i < n; i++){
- if(nums[i] % 2 === 0) even[evenIndex++] = nums[i];
- else odd[oddIndex++] = nums[i];
- }
- // 把奇偶數組重新存回 nums
- let index = 0;
- for(let i = 0; i < even.length; i++){
- nums[index++] = even[i];
- nums[index++] = odd[i];
- }
- return nums;
- };
- //方法二
- var sortArrayByParityII = function(nums) {
- const n = nums.length;
- const result = new Array(n);
- // 偶數下標 和 奇數下標
- let evenIndex = 0, oddIndex = 1;
- for(let i = 0; i < n; i++){
- if(nums[i] % 2 === 0) {
- result[evenIndex] = nums[i];
- evenIndex += 2;
- } else {
- result[oddIndex] = nums[i];
- oddIndex += 2;
- }
- }
- return result;
- };
- //方法三
- var sortArrayByParityII = function(nums) {
- let oddIndex = 1;
- for(let i = 0; i < nums.length; i += 2){
- if(nums[i] % 2 === 1){ // 在偶數位遇到了奇數
- while(nums[oddIndex] % 2 !== 0) oddIndex += 2;// 在奇數位找一個偶數
- [nums[oddIndex], nums[i]] = [nums[i], nums[oddIndex]]; // 解構賦值交換
- }
- }
- return nums;
- };