自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

數(shù)據(jù)結(jié)構(gòu)與算法之按奇偶排序數(shù)組II

開發(fā) 前端 算法
一道簡(jiǎn)單模擬題,來做做看!給定一個(gè)非負(fù)整數(shù)數(shù)組 A, A 中一半整數(shù)是奇數(shù),一半整數(shù)是偶數(shù)。對(duì)數(shù)組進(jìn)行排序,以便當(dāng) A[i] 為奇數(shù)時(shí),i 也是奇數(shù);當(dāng) A[i] 為偶數(shù)時(shí), i 也是偶數(shù)。

[[429517]]

一道簡(jiǎn)單模擬題,來做做看!

按奇偶排序數(shù)組II

力扣題目鏈接:https://leetcode-cn.com/problems/sort-array-by-parity-ii/

給定一個(gè)非負(fù)整數(shù)數(shù)組 A, A 中一半整數(shù)是奇數(shù),一半整數(shù)是偶數(shù)。

對(duì)數(shù)組進(jìn)行排序,以便當(dāng) A[i] 為奇數(shù)時(shí),i 也是奇數(shù);當(dāng) A[i] 為偶數(shù)時(shí), i 也是偶數(shù)。

你可以返回任何滿足上述條件的數(shù)組作為答案。

示例:

  • 輸入:[4,2,5,7]
  • 輸出:[4,5,2,7]
  • 解釋:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也會(huì)被接受。

思路

這道題目直接的想法可能是兩層for循環(huán)再加上used數(shù)組表示使用過的元素。這樣的的時(shí)間復(fù)雜度是O(n^2)。

方法一

其實(shí)這道題可以用很樸實(shí)的方法,時(shí)間復(fù)雜度就就是O(n)了,C++代碼如下:

  1. class Solution { 
  2. public
  3.     vector<int> sortArrayByParityII(vector<int>& A) { 
  4.         vector<int> even(A.size() / 2); // 初始化就確定數(shù)組大小,節(jié)省開銷 
  5.         vector<int> odd(A.size() / 2); 
  6.         vector<int> result(A.size()); 
  7.         int evenIndex = 0; 
  8.         int oddIndex = 0; 
  9.         int resultIndex = 0; 
  10.         // 把A數(shù)組放進(jìn)偶數(shù)數(shù)組,和奇數(shù)數(shù)組 
  11.         for (int i = 0; i < A.size(); i++) { 
  12.             if (A[i] % 2 == 0) even[evenIndex++] = A[i]; 
  13.             else odd[oddIndex++] = A[i]; 
  14.         } 
  15.         // 把偶數(shù)數(shù)組,奇數(shù)數(shù)組分別放進(jìn)result數(shù)組中 
  16.         for (int i = 0; i < evenIndex; i++) { 
  17.             result[resultIndex++] = even[i]; 
  18.             result[resultIndex++] = odd[i]; 
  19.         } 
  20.         return result; 
  21.     } 
  22. }; 
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(n)

方法二

以上代碼我是建了兩個(gè)輔助數(shù)組,而且A數(shù)組還相當(dāng)于遍歷了兩次,用輔助數(shù)組的好處就是思路清晰,優(yōu)化一下就是不用這兩個(gè)輔助樹,代碼如下:

  1. class Solution { 
  2. public
  3.     vector<int> sortArrayByParityII(vector<int>& A) { 
  4.         vector<int> result(A.size()); 
  5.         int evenIndex = 0;  // 偶數(shù)下表 
  6.         int oddIndex = 1;   // 奇數(shù)下表 
  7.         for (int i = 0; i < A.size(); i++) { 
  8.             if (A[i] % 2 == 0) { 
  9.                 result[evenIndex] = A[i]; 
  10.                 evenIndex += 2; 
  11.             } 
  12.             else { 
  13.                 result[oddIndex] = A[i]; 
  14.                 oddIndex += 2; 
  15.             } 
  16.         } 
  17.         return result; 
  18.     } 
  19. }; 
  • 時(shí)間復(fù)雜度O(n)
  • 空間復(fù)雜度O(n)

方法三

當(dāng)然還可以在原數(shù)組上修改,連result數(shù)組都不用了。

  1. class Solution { 
  2. public
  3.     vector<int> sortArrayByParityII(vector<int>& A) { 
  4.         int oddIndex = 1; 
  5.         for (int i = 0; i < A.size(); i += 2) { 
  6.             if (A[i] % 2 == 1) { // 在偶數(shù)位遇到了奇數(shù) 
  7.                 while(A[oddIndex] % 2 != 0) oddIndex += 2; // 在奇數(shù)位找一個(gè)偶數(shù) 
  8.                 swap(A[i], A[oddIndex]); // 替換 
  9.             } 
  10.         } 
  11.         return A; 
  12.     } 
  13. }; 
  • 時(shí)間復(fù)雜度:O(n)
  • 空間復(fù)雜度:O(1)

這里時(shí)間復(fù)雜度并不是O(n^2),因?yàn)榕紨?shù)位和奇數(shù)位都只操作一次,不是n/2 * n/2的關(guān)系,而是n/2 + n/2的關(guān)系!

其他語言版本

Java

  1. // 方法一 
  2. class Solution { 
  3.     public int[] sortArrayByParityII(int[] nums) { 
  4.         // 分別存放 nums 中的奇數(shù)、偶數(shù) 
  5.         int len = nums.length; 
  6.         int evenIndex = 0; 
  7.         int oddIndex = 0; 
  8.         int[] even = new int[len / 2]; 
  9.         int[] odd = new int[len / 2]; 
  10.         for (int i = 0; i < len; i++) { 
  11.             if (nums[i] % 2 == 0) { 
  12.                 even[evenIndex++] = nums[i]; 
  13.             } else { 
  14.                 odd[oddIndex++] = nums[i]; 
  15.             } 
  16.         } 
  17.         // 把奇偶數(shù)組重新存回 nums 
  18.         int index = 0; 
  19.         for (int i = 0; i < even.length; i++) { 
  20.             nums[index++] = even[i]; 
  21.             nums[index++] = odd[i]; 
  22.         } 
  23.         return nums; 
  24.     } 

Python3

  1. #方法2 
  2. class Solution: 
  3.     def sortArrayByParityII(self, nums: List[int]) -> List[int]: 
  4.         result = [0]*len(nums) 
  5.         evenIndex = 0 
  6.         oddIndex = 1 
  7.         for i in range(len(nums)): 
  8.             if nums[i] % 2: #奇數(shù) 
  9.                 result[oddIndex] = nums[i] 
  10.                 oddIndex += 2 
  11.             else: #偶數(shù) 
  12.                 result[evenIndex] = nums[i] 
  13.                 evenIndex += 2 
  14.         return result 
  15.  
  16. #方法3 
  17. class Solution: 
  18.     def sortArrayByParityII(self, nums: List[int]) -> List[int]: 
  19.         oddIndex = 1 
  20.         for i in range(0,len(nums),2): #步長(zhǎng)為2 
  21.             if nums[i] % 2: #偶數(shù)位遇到奇數(shù) 
  22.                 while  nums[oddIndex] % 2: #奇數(shù)位找偶數(shù) 
  23.                     oddIndex += 2 
  24.                 nums[i], nums[oddIndex] = nums[oddIndex], nums[i] 
  25.         return nums 

Go

  1. // 方法一 
  2. func sortArrayByParityII(nums []int) []int { 
  3.  // 分別存放 nums 中的奇數(shù)、偶數(shù) 
  4.  even, odd := []int{}, []int{} 
  5.  for i := 0; i < len(nums); i++ { 
  6.   if (nums[i] % 2 == 0) { 
  7.    even = append(even, nums[i]) 
  8.   } else { 
  9.    odd = append(odd, nums[i]) 
  10.   } 
  11.  } 
  12.  
  13.  // 把奇偶數(shù)組重新存回 nums 
  14.  result := make([]int, len(nums)) 
  15.  index := 0 
  16.  for i := 0; i < len(even); i++ { 
  17.   result[index] = even[i]; index++; 
  18.   result[index] = odd[i]; index++; 
  19.  } 
  20.  return result; 

JavaScript

  1. //方法一 
  2. var sortArrayByParityII = function(nums) { 
  3.     const n = nums.length; 
  4.     // 分別存放 nums 中的奇數(shù)、偶數(shù) 
  5.     let evenIndex = 0, oddIndex = 0; 
  6.     // 初始化就確定數(shù)組大小,節(jié)省開銷 
  7.     const even = new Array(Math.floor(n/2)); 
  8.     const odd = new Array(Math.floor(n/2)); 
  9.     // 把A數(shù)組放進(jìn)偶數(shù)數(shù)組,和奇數(shù)數(shù)組 
  10.     for(let i = 0; i < n; i++){ 
  11.         if(nums[i] % 2 === 0) even[evenIndex++] = nums[i]; 
  12.         else odd[oddIndex++] = nums[i]; 
  13.     } 
  14.     // 把奇偶數(shù)組重新存回 nums 
  15.     let index = 0; 
  16.     for(let i = 0; i < even.length; i++){ 
  17.         nums[index++] = even[i]; 
  18.         nums[index++] = odd[i]; 
  19.     } 
  20.     return nums; 
  21. }; 
  22.  
  23. //方法二 
  24. var sortArrayByParityII = function(nums) { 
  25.     const n = nums.length; 
  26.     const result = new Array(n); 
  27.     // 偶數(shù)下標(biāo) 和 奇數(shù)下標(biāo) 
  28.     let evenIndex = 0, oddIndex = 1; 
  29.     for(let i = 0; i < n; i++){ 
  30.         if(nums[i] % 2 === 0) { 
  31.             result[evenIndex] = nums[i]; 
  32.             evenIndex += 2; 
  33.         } else { 
  34.             result[oddIndex] = nums[i]; 
  35.             oddIndex += 2; 
  36.         } 
  37.     } 
  38.     return result; 
  39. }; 
  40.  
  41. //方法三 
  42. var sortArrayByParityII = function(nums) { 
  43.     let oddIndex = 1; 
  44.     for(let i = 0; i < nums.length; i += 2){ 
  45.         if(nums[i] % 2 === 1){ // 在偶數(shù)位遇到了奇數(shù) 
  46.             while(nums[oddIndex] % 2 !== 0) oddIndex += 2;// 在奇數(shù)位找一個(gè)偶數(shù) 
  47.             [nums[oddIndex], nums[i]] = [nums[i], nums[oddIndex]]; // 解構(gòu)賦值交換 
  48.         } 
  49.     } 
  50.     return nums; 
  51. }; 

 

責(zé)任編輯:姜華 來源: 代碼隨想錄
相關(guān)推薦

2023-03-07 08:02:07

數(shù)據(jù)結(jié)構(gòu)算法數(shù)列

2022-01-18 19:13:52

背包問題數(shù)據(jù)結(jié)構(gòu)算法

2021-07-16 04:57:45

Go算法結(jié)構(gòu)

2023-03-02 08:15:13

2023-03-10 08:07:39

數(shù)據(jù)結(jié)構(gòu)算法計(jì)數(shù)排序

2023-04-27 09:13:20

排序算法數(shù)據(jù)結(jié)構(gòu)

2023-03-13 10:08:31

數(shù)據(jù)結(jié)構(gòu)算法

2019-03-29 09:40:38

數(shù)據(jù)結(jié)構(gòu)算法前端

2023-03-06 08:10:52

數(shù)據(jù)結(jié)構(gòu)算法數(shù)據(jù)

2021-04-15 09:36:44

Java數(shù)據(jù)結(jié)構(gòu)算法

2020-10-30 09:56:59

Trie樹之美

2022-09-21 07:57:33

二叉搜索樹排序二叉樹

2022-09-26 07:56:53

AVL算法二叉樹

2021-03-23 08:33:22

Java數(shù)據(jù)結(jié)構(gòu)算法

2020-10-21 14:57:04

數(shù)據(jù)結(jié)構(gòu)算法圖形

2021-03-08 06:28:57

JAVA數(shù)據(jù)結(jié)構(gòu)與算法稀疏數(shù)組

2020-10-20 08:14:08

算法與數(shù)據(jù)結(jié)構(gòu)

2023-03-08 08:03:09

數(shù)據(jù)結(jié)構(gòu)算法歸并排序

2020-12-31 05:31:01

數(shù)據(jù)結(jié)構(gòu)算法

2017-06-16 09:22:22

數(shù)據(jù)結(jié)構(gòu)算法鏈表
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)