【排序算法运用】剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

本问题对应课程链接:剑指offer刷题第三期

要求:
1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 22 个回答

  • 九号球 普通 2023年11月25日 上午10:54

    “`java
    //双指针
    class Solution {
    public int[] trainingPlan(int[] actions) {
    if (actions == null || actions.length == 0) {
    return actions;
    }
    //使用双指针,从前面找偶数,从后面找奇数,互换
    for (int i = 0, j = actions.length – 1; i < j; ) { while ((actions[i] & 1) == 1 && i < j) { i++; } while ((actions[j] & 1) == 0 && i < j) { j--; } if (i < j) { int tmp = actions[i]; actions[i] = actions[j]; actions[j] = tmp; i++; j--; } } return actions; } }

    0 赞同 0 条回复
  • 招舟子 普通 2023年9月8日 下午12:59

    class Solution {
    public int[] exchange(int[] nums) {
    int i=0,j=nums.length-1;
    while(i<j){
    while(i<j&&nums[i]%2!=0){
    i++;
    }
    while(i<j&&nums[j]%20){
    j–;
    }
    if(i<j){
    int tmp=nums[i];
    nums[i] = nums[j];
    nums[j]=tmp;
    i++;
    j–;
    }
    }
    return nums;
    }
    }
    时间on

    0 赞同 0 条回复
  • jhfuture 普通 2023年9月5日 上午11:04

    “`java
    时间复杂度为O(n),空间复杂度为1
    class Solution {
    public int[] exchange(int[] nums) {
    // 使用双指针的思想即可。
    // 左边指针寻找偶数,右边指针寻找偶数,当遇到相同的时候,则进行交换,然后继续找
    if (nums==null||nums.length==0){
    return nums;
    }
    int left=0;
    int right=nums.length-1;

    while(left<right){
        // 开始找
        while(nums[left]%2!=0 &&left<right) left++;
        while( nums[right]%2==0 && left<right) right--;
    

    // 保存左边的,然后把右边放入左边,然后再将tmp放入右边
    int temp=nums[left];
    nums[left]=nums[right];
    nums[right]=temp;
    }

    return nums;
    
    
    }
    

    }

    0 赞同 0 条回复
  • 屠龙手段 普通 2023年7月30日 下午7:23

    双指针,时间复杂度O(n) 空间复杂度O(1)

    class Solution:
        def exchange(self, nums: List[int]) -> List[int]:
            left, right = 0, len(nums) - 1
            while left < right:
                while left < right and nums[left] & 1 == 1: left += 1
                while left < right and nums[right] & 1 == 0: right -= 1
                if left < right:
                    nums[left] = nums[left] ^ nums[right]
                    nums[right] = nums[left] ^ nums[right]
                    nums[left] = nums[left] ^ nums[right]
            return nums
    
    0 赞同 0 条回复
  • 怀瑾 永久会员 2023年7月19日 下午3:40

    “`C++
    void exchangeOddEven(vector& nums) {
    int left = 0; // 左指针从数组起始位置出发
    int right = nums.size() – 1; // 右指针从数组末尾位置出发

    while (left < right) {
        // 先找到第一个偶数
        while (left < right && nums[left] % 2 != 0)
            left++;
    
        // 再找到第一个奇数
        while (left < right && nums[right] % 2 == 0)
            right--;
    
        // 如果找到了,交换左右指针所指的元素
        if (left < right)
            std::swap(nums[left], nums[right]);
    }
    

    }

    “`

    0 赞同 0 条回复
  • BOZ 普通 2023年7月18日 上午10:37

    【思路】类似快排,利用双指针在一次遍历数组的过程中交换左边的偶数与右边的奇数
    【复杂度】时间O(n),空间O(1)

    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            int l = 0, r = nums.size() - 1;
            while(r > l) {
                // move l
                while (nums[l] & 1 && r > l) l++;
                // move r
                while (!(nums[r] & 1) && r > l) r--;
                // exchange
                if (r > l) std::swap(nums[l], nums[r]);
                // printf("l = %d, r = %d\n", l, r);
            }
            return nums;
        }
    };
    
    0 赞同 0 条回复
  • 江畅 普通 2023年7月16日 下午3:22
    // 调整数组顺序使奇数位于偶数前面
    // 时间复杂度 O(N)
    // 空间复杂度 O(1)
    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            if (nums.empty()) { return nums; }
    
            int left = 0;
            int right = nums.size() - 1;
    
            while (left < right) {
                // nums[left] 为奇数
                while (left < right && nums[left] % 2 == 1) { ++left; }
    
                // nums[right] 为偶数
                while (left < right && nums[right] % 2 == 0) { --right; }
    
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
            return nums;
        }
    };
    
    0 赞同 0 条回复
  • 哈哈哈哈开开心心 普通 2023年7月16日 上午7:02
    class Solution:
        def exchange(self, nums: List[int]) -> List[int]:
            if len(nums) == 0:
                return nums
    
            l, r = 0, len(nums) - 1
    
            while l < r:
                while l < r and nums[l] % 2 == 1:
                    l += 1
    
                while l < r and nums[r] % 2 == 0:
                    r -= 1
    
                tmp = nums[l]
                nums[l] = nums[r]
                nums[r] = tmp
    
            return nums
    

    快排

    0 赞同 0 条回复
  • WhyNot 普通 2023年7月15日 下午11:27
    int* exchange(int* nums, int numsSize, int* returnSize){
     if(numsSize<0||nums==NULL) return NULL;
     int left=0,right=numsSize-1;
     while(left<right)
     {
      while(left<right&&nums[left]%2==1)
      {
          left++;
      }
      while(left<right&&nums[right]%2==0)
      {
          right--;
      }
      int temp=nums[left];
      nums[left]=nums[right];
      nums[right]=temp;
     }
     *returnSize=numsSize;
     return nums;
    }
    

    时间复杂度为O(numsSize),空间复杂度为O(1)

    0 赞同 0 条回复
  • 花老爷 普通 2023年7月15日 下午1:26

    双指针
    时间:O(n)
    空间:O(1)

    class Solution {
        public int[] exchange(int[] nums) {
            int l = 0, r = nums.length - 1;
            while (true) {
                while (l < r && nums[l] % 2 == 1) {
                    l++;
                }
                while (l < r && nums[r] % 2 == 0) {
                    r--;
                }
                if (l >= r) {
                    break;
                }
                int temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
            }
            return nums;
        }
    }
    
    0 赞同 0 条回复
  • Ming 普通 2023年7月14日 下午8:25

    快排,从左寻找第一个偶数的下标,从右寻找第一个奇数的下标,进行交互,重复

    class Solution {
        public int[] exchange(int[] nums) {
            if(nums == null || nums.length <= 1) {
                return nums;
            }
            int left = 0;
            int right = nums.length - 1;
            while(left < right) {
                while(left < right && nums[left] % 2 != 0) left++;
                while(left < right && nums[right] % 2 != 1) right--;
    
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
            return nums;
        }
    }
    
    0 赞同 0 条回复
  • 行动派小彩笔 普通 2023年7月13日 下午7:50
    //时间复杂度O(n),空间复杂度O(1)
    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            int j =nums.size()-1,i=0;
            while(i<j)
            {
                if(nums[i]&1) ++i;
                else swap(nums[i],nums[j--]);;
            }
            return nums;
        }
    };
    
    1 赞同 0 条回复
  • Vow the vow。 普通 2023年7月13日 上午10:39

    双指针。

    public int[] exchange(int[] nums) {
            int n = nums.length;
            int l = 0, r = n-1;
            while(l < r){
                while(l < r && nums[l] % 2 == 1) l++;
                while(l < r && nums[r] % 2 == 0) r--;
                if(l < r){
                    nums[l] ^= nums[r];
                    nums[r] ^= nums[l];
                    nums[l] ^= nums[r];
                }
            }
            return nums;
        }
    
    0 赞同 0 条回复
  • Jerry 普通 2023年7月12日 下午11:28
    // 双指针
    class Solution {
        public int[] exchange(int[] nums) {
            int left = 0, right = nums.length - 1, tmp;
            while (left < right) {
                while (left < right && (nums[left] & 1) == 1) left++; //left遇到奇数,则执行 left++,进行跳过
                while (left < right && (nums[right] & 1) == 0) right--; //right遇到偶数,则执行 right--,进行跳过
    
                // 交互两数
                tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
            return nums;
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • yxy 普通 2023年7月12日 下午11:27

    时间复杂度O(n)

    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            int sz = nums.size();
            int l = -1, r = sz;
            while(l<r)
            {
                do 
                    l++;
                while(l<r&&nums[l]%2==1);
                do 
                    r--;
                while(l<r&&nums[r]%2==0);
                if(l<r)
                    swap(nums[l], nums[r]);
            }
            return nums;
        }
    };
    
    0 赞同 0 条回复
  • 局外人 普通 2023年7月12日 下午11:10

    思想:按照快排的思想,交换两侧数字,从大小数字,变成奇偶数字
    时间复杂度O(n) 空间复杂度O(1)

    class Solution {
        public int[] exchange(int[] nums) {
            int left=0, right=nums.length-1;
            while(left<right){
                while(left<right && nums[left]%2==1)   left++;
                while(left<right && nums[right]%2==0)  right--;
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
            return nums;
        }
    }
    
    0 赞同 0 条回复
  • 张君雅小妹妹 普通 2023年7月12日 下午9:43

    时间复杂度:O(n)
    空间复杂度:O(1)

    class Solution {
        public int[] exchange(int[] nums) {
            if(nums == null || nums.length <= 1) return nums;
    
            int i = 0, j = nums.length - 1;
            while(i < j){
                while(i < j && nums[i] % 2 == 1){
                    i++;
                }
    
                while(i < j && nums[j] % 2 == 0){
                    j--;
                }
                if(i < j){
                    swap(nums, i, j);
                }
            }
    
            return nums;
        }
    
    
    
        public void swap(int[] nums, int i, int j){
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
    }
    
    0 赞同 0 条回复
  • 小吕 普通 2023年7月12日 下午8:09

    c++实现,结果不计入空间复杂度
    vector exchange(vector& nums) {
    int len = nums.size();
    vector
    res(len);
    int left = 0,right=len-1;
    for(int num:nums)
    {
    if(num%21)
    {
    res[left++]=num;
    }
    else res[right–]=num;
    }
    return res;
    }

    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年7月12日 下午5:01
    //时间O(n) 空间O(1)
    public int[] exchange(int[] nums) {
    
            int index = nums.length - 1;
    
            for(int i = 0;i <= index;i++){
                if((nums[i] & 1) == 0){
                    //偶数,去后面
                    int temp = nums[i];
                    nums[i] = nums[index];
                    nums[index] = temp;
    
                    index--;  
                    i--;
                }
            }
    
            return nums;
        }
    
    0 赞同 0 条回复
  • 张开怀 普通 2023年7月12日 下午3:53
    class Solution{
    public:
           vector<int> exchange(vector<int>& nums){
                  if (nums.empty()){
                         reutrn nums;
                  }
                  int left=0;
                  int right= nums.zize()-1;
                  while(left < right){
                         while(left <right && nums[left]%2 !=0) left--;
                         while(left <right && nums[right]%2 !=1) right--;
    
                         int temp = nums[left];
                         nums[left] = nums[right];
                         nums[right] = temp;
                  }
                  return nums;
           }
    
    };
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • Dsjh 普通 2023年7月12日 下午3:25

    MY

    快排思想

    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            if(nums.size()<2){
                return nums;
            }
            int i=-1,j=nums.size();
            while(i<j){
                do i++; while((i<j)&&(nums[i]&1)==1);
                do j--; while((i<j)&&(nums[j]&1)!=1);
                if(i<j)
                    swap(nums[i],nums[j]);
            }
            return nums;
        }
    };
    
    0 赞同 0 条回复
  • Yodl. 普通 2023年7月12日 下午2:41
    //时间复杂度O(n) 空间复杂度O(1)
    class Solution {
    public:
        vector<int> exchange(vector<int>& nums) {
            if(nums.size()==0)
            {
                return {};
            }
            int i,j,tmp;
            i=0,j=nums.size()-1,tmp=nums[nums.size()-1];
            while(i<j)
            {
                while(i<j&&nums[i]%2==1)
                {
                    i++;
                }
                if(i<j)
                {
                    nums[j]=nums[i];
                    j--;
                }
                while(i<j&&nums[j]%2==0)
                {
                    j--;
                }
                if(i<j)
                {
                    nums[i]=nums[j];
                    i++;
                }
            }
            nums[i]=tmp;
            return nums;
        }
    };
    
    0 赞同 0 条回复

# 回答此问题

后才能回答