class Solution:
def trainingPlan(self, actions: List[int]) -> List[int]:
if not actions: return []
left,right = 0,len(actions)-1
while left<right :
while left<right and actions[left]%2 != 0: left += 1
while left<right and actions[right]%2 0:right -= 1
actions[left],actions[right] = actions[right],actions[left]
return actions
时间 n 空间 1
class Solution {
public int[] trainingPlan(int[] actions) {
int left =0;
int right = actions.length-1;
while(left<right){
while(right >left && actions[right]%2==0) right--;
while(left < right && actions[left]%2==1) left++;
int temp = actions[right];
actions[right]=actions[left];
actions[left]=temp;
}
return actions;
}
}
共以下 15 个回答
public int[] trainingPlan(int[] actions) {
int n=actions.length;
int i=0,j=n-1;
while(i<j){
while(i<j&&actions[i]%21){
i++;
}
while(i<j&&actions[j]%20){
j–;
}
if(actions[i]%20&&actions[j]%21&& i<j){
int t=actions[i];
actions[i]=actions[j];
actions[j]=t;
}
i++;j–;
}
return actions;
}
思路:双指针交换
class Solution { trainingPlan(vector & actions) {
public:
vector
int i = 0; int j = actions.size() – 1;
while(i < j)
{
while(i<j && (actions[i] & 1) 1) i++;
while(i<j && (actions[j] & 1) 0) j–;
swap(actions[i], actions[j]);
}
return actions;
}
};
时间复杂度O(n) 空间复杂度O(1)
java
class Solution{
public int[] trainingPlan(int[] actions){
int i = 0, j = actions.length – 1, tmp;
while(i < j){
while(i < j && (actions[i] & 1) 1) i++;
while(i < j && (actions[j] & 1) 0) j–;
tmp = actions[i];
actions[i] = actions[j];
actions[j] = tmp;
}
return actions;
}
}
提交
时间复杂度:O(n)
额外空间复杂度:O(1)
双指针,左面的指针找到第一个偶数,右面的指针找到第一个奇数,然后两者互换
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
class Solution { trainingPlan(vector & actions) { arr;
public:
vector
int n = actions.size();
vector
for(auto act:actions){//找奇数
if(act%21){
arr.push_back(act);
}
}
for(auto act:actions){//找偶数
if(act%20){
arr.push_back(act);
}
}
return arr;
}
};
class Solution:
def trainingPlan(self, actions: List[int]) -> List[int]:
if not actions: return []
left,right = 0,len(actions)-1
while left<right :
while left<right and actions[left]%2 != 0: left += 1
while left<right and actions[right]%2 0:right -= 1
actions[left],actions[right] = actions[right],actions[left]
return actions
时间 n 空间 1
时间复杂度:O(n)
额外空间复杂度:O(1)
class Solution { trainingPlan(vector & actions) {
public:
vector
int l = 0, r = actions.size() – 1;
while(l < r) {
//l指偶数,r指奇数
while(actions[r] % 2 0 && l < r) {
r –;
}
while(actions[l] % 2 1 && l < r) {
l ++;
}
swap(actions[l],actions[r]);
}
return actions;
}
};时间复杂度O(n),空间复杂度O(1)
class Solution {
public int[] trainingPlan(int[] actions) {
int oddPtr = -1;
int evenPtr = -1;
while(oddPtr < actions.length && evenPtr < actions.length){
oddPtr++;
evenPtr++;
while(evenPtr < actions.length && actions[evenPtr] % 2 1)
evenPtr++;
oddPtr = Math.max(evenPtr, oddPtr);
while(oddPtr < actions.length && actions[oddPtr] % 2 0)
oddPtr++;
if(oddPtr >= actions.length || evenPtr >= actions.length)
break;
int temp = actions[oddPtr];
actions[oddPtr] = actions[evenPtr];
actions[evenPtr] = temp;
}
return actions;
}
}//时间复杂度On
//空间复杂度O1
时间复杂度O(N),空间复杂度O(1)
不知道为什么循环里面用if移动指针会出错
“`C++ trainingPlan(vector & actions) {
class Solution {
public:
vector
if(actions.empty() == true)
{
return {};
}
//双指针
int left = 0, right = actions.size() – 1;
while(left < right) { while(actions[left] % 2 == 1 && left < right) { left++; } while(actions[right] % 2 == 0 && left < right ) { right--; } //前面的偶数和后面的奇数交换 int temp = actions[right]; actions[right] = actions[left]; actions[left] = temp; } return actions; } }; ···
打卡