输入一个长度为 n 整数数组,数组里面可能含有相同的元素,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前面部分,所有的偶数位于数组的后面部分,对奇数和奇数,偶数和偶数之间的相对位置不做要求,但是时间复杂度和空间复杂度必须如下要求。
数据范围:,数组中每个数的值
要求:时间复杂度 ,空间复杂度
[1,2,3,4]
[1,3,2,4]
[3,1,2,4]或者[3,1,4,2]也是正确答案
[1,3,5,6,7]
[1,3,5,7,6]
[3,1,5,7,6]等也是正确答案
[1,4,4,3]
[1,3,4,4]
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型一维数组 */ public int[] reOrderArrayTwo (int[] array) { // write code here int low=0,high=array.length-1; while(low<high){ while(low<high&&array[high]%2==0) high--; while (low<high&&array[low]%2==1) low++; int temp=array[low]; array[low]=array[high]; array[high]=temp; low++; high--; } return array; } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型一维数组 */ public int[] reOrderArrayTwo(int[] array) { // write code here // 奇数指针,从前往后遍历 int low = 0; //偶数指针,从后往前遍历 int high = array.length - 1; int temp = 0; while (low < high) { //左右都是奇数,左移右不动 if (array[low] % 2 == 1 && array[high] % 2 == 1) { low++; } // 左奇数,右偶数 else if (array[low] % 2 == 1 && array[high] % 2 == 0) { low++; high--; } // 左偶数,右奇数,交换位置 else if (array[low] % 2 == 0 && array[high] % 2 == 1) { temp = array[low]; array[low] = array[high]; array[high] = temp; } // 左右都是偶数 else { high--; } } return array; } }
from typing import List class Solution: def reOrderArrayTwo(self , array: List[int]) -> List[int]: # write code here idx=1 n=len(array) l=0 while l < n-idx: if array[l]&1: l+=1 else: array[l],array[-idx]=array[-idx],array[l] # 偶数换到队尾 idx+=1 return array
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型一维数组 * @return int整型一维数组 */ public int[] reOrderArrayTwo (int[] array) { if(array.length==0||array.length==1){ return array; } int left=0; int right=array.length-1; while(left<right){ if(array[left]%2==0&&array[right]%2!=0){ int temp=array[left]; array[left]=array[right]; array[right]=temp; left++; right--; } if(array[left]%2!=0){ left++; } if(array[right]%2==0){ right--; } } return array; } }
public int[] reOrderArrayTwo (int[] array) { // write code here for(int i = 0, j = 0; i < array.length; i++) { if(array[i] % 2 != 0) { int temp = array[i]; array[i] = array[j]; array[j++] = temp; } } return array; }
public int[] reOrderArrayTwo(int[] array) { int l = 0, r = array.length - 1, tmp = 0; // 终止条件:左右指针相遇 while (l < r) { if ((array[l] & 1) == 1) { // 奇数:左指针往右挪 l++; } else { // 偶数:交换后,右指针往左挪 tmp = array[l]; array[l] = array[r]; array[r] = tmp; r--; } } return array; }
class Solution { public: vector<int> reOrderArrayTwo(vector<int>& array) { int l = -1, r = array.size(); if (r == 0) return array; while (l < r) { while(array[++l] % 2 == 1); while(array[--r] % 2 == 0) ; if (l < r) swap(array[l], array[r]); } return array; } };类似于快排思想
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vector<int> reOrderArrayTwo(vector<int>& array) { // 时间复杂度O(N),空间复杂度O(1) int left = 0, right = array.size() - 1; while (left < right) { while (array[left] % 2) ++left; if (left >= right) break; while (array[right] % 2 == 0) --right; if (left >= right) break; swap(array[left++], array[right--]); } return array; } };
class Solution: def reOrderArrayTwo(self , array: List[int]) -> List[int]: # write code here n = len(array) i=0 while i<n: if array[i]%2==0: array.append(array[i]) array.pop(i) n -=1 else: i +=1 return array
class Solution { public: void exchangeNum(int &i, int &j){ int temp = i; i = j; j = temp; } vector<int> reOrderArrayTwo(vector<int>& array) { int p1 = 0; int p2 = array.size() - 1; while(p1 < p2){ if(array[p1]%2 == 0){ if(array[p2]%2 != 0){ exchangeNum(array[p1], array[p2]); p1++; p2--; }else{ p2--; } }else { p1++; } } return array; } };
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vector<int> reOrderArrayTwo(vector<int>& array) { // write code here int size = array.size(); vector<int> odd; vector<int> even; vector<int> res; for(int i = 0; i < size; i++){ if(array[i] % 2 == 1) odd.push_back(array[i]); else even.push_back(array[i]); } res.insert(res.begin(), odd.begin(), odd.end()); res.insert(res.end(), even.begin(), even.end()); return res; } };