【编程题】荷兰国旗问题(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;
}
}