【编程题】荷兰国旗问题(java实现)

【编程题】荷兰国旗问题(java实现)

题目描述

给定一个数组arr和一个数num,请把小于num是数放在数组的左边,等于num的数放在数组的中间,大于num是数放在数组的右边,要求额外空间为o(1),时间为o(n)
例如:
arr={4,5,6,7,5,1,2,3}
num=5
输出
0, 3, 2, 1, 5, 5, 7, 6

代码

import java.util.Arrays;

public class HeLanGuoQi {
    public static void main(String[] args){
        int[] arr={4,5,6,7,5,1,2,3};
        rotate(arr,5);
        System.out.println(Arrays.toString(arr));
    }
    static void rotate(int[] arr,int num){
        if(arr==null||arr.length<=0)return;
        int left=-1,right=arr.length,current=0;
        while (current<right){
            if(arr[current]<num){
                left++;
             swap(arr,left,current);
             current++;
            }
            else if(arr[current]==num){
                current++;
            }else {
                right--;
                swap(arr,current,right);
            }
        }
    }
    static void swap(int[] arr,int i,int j){
        int temp=arr[j];
        arr[j]=arr[i];
        arr[i]=temp;
    }
}

改造题目

给定一个数组arr和一个数num,请把小于等于num是数放在数组的左边大于num是数放在数组的右边,要求额外空间为o(1),时间为o(n)
例如:
arr={4,5,6,7,5,1,2,5}
num=5
输出
0, 0, 5, 1, 2, 5, 6, 7

代码

import java.util.Arrays;
public class NewHeLan {
    public static void main(String[] args){
        int[] arr={4,5,6,7,5,1,2,5};
        rotate(arr,5);
        System.out.println(Arrays.toString(arr));
    }
    static void rotate(int[] arr,int num){
        if(arr==null||arr.length<=0)return;
        int left=-1,current=0,len=arr.length;
        while (current<len){
            if(arr[current]<=num){
                left++;
                swap(arr,left,current);
                current++;
            }
           else {
                current++;
            }
        }
    }
    static void swap(int[] arr,int i,int j){
       int temp=arr[j];
        arr[j]=arr[i];
        arr[i]=temp;
    }
}

改题目

快排的partition函数
从数组的l位置到r位置,实现荷兰国旗问题,返回等于区域的左右边界
代码

import java.util.Arrays;

public class Partition {
    public static void main(String[] args){
        int[] arr={4,5,6,7,5,1,2,3};
        System.out.println(Arrays.toString(partition(arr,0,arr.length-1,5)));
        System.out.println(Arrays.toString(arr));
    }
    static int[] partition(int[] arr,int low,int high,int num){
        if(arr==null||arr.length<=0)return null;
        int left=low-1,right=high+1,current=low;
        while (current<right){
            if(arr[current]<num){
                left++;
                swap(arr,left,current);
                current++;
            }
            else if(arr[current]==num){
                current++;
            }else {
                right--;
                swap(arr,current,right);
            }
        }
        return new int[] {left+1,right-1};
    }
    static void swap(int[] arr,int i,int j){
         int temp=arr[j];
        arr[j]=arr[i];
        arr[i]=temp;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值