LeetCode-面试题21. 调整数组顺序使奇数位于偶数前面

https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/

public int[] exchange(int[] nums) {
		int n=nums.length;
		int i=0,j=n-1;
		while(i<j) {
			while(i<j && isOdd(nums[i])) i++;
			while(i<j && isEven(nums[j])) j--;
			nums= exchangeArr(nums, i, j);
		}
		return nums;
    }
	
	int[] exchangeArr(int[] nums,int i,int j) {
		int temp=nums[i];
		nums[i]=nums[j];
		nums[j]=temp;
		return nums;
	}
	
	boolean isOdd(int i){
		if(i%2==1) return true;
		return false;
	}
	
	boolean isEven(int i){
		if(i%2==0) return true;
		return false;
	}
发布了137 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/m0_37302219/article/details/104960531