LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列。

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:

You are not suppose to use the library‘s sort function for this problem.

click to show follow up.

Follow up:

A rather straight forward solution is a two-pass algorithm using counting sort.

First, iterate the array counting number of 0‘s, 1‘s, and 2‘s, then overwrite array with total number of 0‘s, then 1‘s and followed by 2‘s.

Could you come up with an one-pass algorithm using only constant space?

想了一段时间,未果,于是前往Discuss区域围观。

帖子网址点击打开链接

网友xianlei给出了一个精妙的方法,我理解之后,写出代码如下,因为有详细的注释,就不啰嗦了。

在草纸上模拟一下,或者单步调试一下,就会明白这个方法的精妙之处!!

public class Solution {
	static int[] A = {1,0};
    public static void sortColors(int[] A) {
    	int i = 0;//last index of array 0
    	int j = 0;//last index of array 1
    	int k = 0;//last index of array 2
    	for(int m = 0; m < A.length; m ++){
    		if(A[m] == 0){ // add 2 to the end of 2 array, replace the 1st 2 with 1, then insert 0 into 0 array,
    			A[k++] = 2;
    			A[j++] = 1;
    			A[i++] = 0;
    		}
    		else if(A[m] == 1){// add 2 to the end of 2 array, then insert 1, at the location of the 1st 2
    			A[k++] = 2;
    			A[j++] = 1;
    		}
    		else{//add 2 to the end of 2 array
    			A[k++] = 2;
    		}
    	}
    }
    public static void main(String args[]){
    	sortColors(A);
    }
}

网友lchen77提供了另一个很好的思路,我的实现代码如下。

因为题目要求按照0,1,2的顺序来排序,所以遇到0,与前面的A[k++]交换,遇到2与A[j--]交换。

需要注意的是,A[k]要么等于0,要么等于1,所以0与A[k++]交换之后,i不用--,整个前面的序列是合法的。

但是2与A[j--]交换之后,i要--,因为从数组尾部随便一个数字与当前的2换完之后,必须还要验证,以防不合法。

而且,循环的终止条件是i,j相遇,相遇的时候,还要执行一次,即循环终止条件是i>j

public class Solution {
	public static void sortColors(int[] A){
		int k = 0; //index for 0 array;
		int j = A.length-1;//index for 2 array
		for(int i = 0; i <= j;i ++){
			if(A[i] == 1){
				continue;
			}
			else if(A[i] == 0){
				A[i] = A[k];
				A[k++] = 0;
			}
			else{
				A[i] = A[j];
				A[j--] = 2;
				i --;
			}
		}
	}
}

LeetCode OJ平台上Sort Colors题目算法探讨,布布扣,bubuko.com

时间: 2024-10-17 08:16:56

LeetCode OJ平台上Sort Colors题目算法探讨的相关文章

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方式

原始题目例如以下,意为寻找数组和最大的子串,返回这个最大和就可以. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum =

【LeetCode OJ 075】Sort Colors

题目链接:https://leetcode.com/problems/sort-colors/ 题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0,

LeetCode 75. 颜色分类(Sort Colors) 30

75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. 注意: 不能使用代码库中的排序函数来解决这道题. 每日一算法2019/6/2Day 30LeetCode75. Sort Colors 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶: 一个直观的解决方

LeetCode OJ 147. Insertion Sort List

Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链表的插入排序,用tmp_tail遍历链表,每次的待插入数是tmp_tail->next的元素,待插入数必须从头开始比较,当然从头开始比较时要注意处理待排序数小于或等于链表首元素的情况,因为插入在链表的首元素之前与一般情况的插入不同,而如果待插入数插入在它之前的数列中,则用于遍历链表的指针tmp_ta

【LeetCode】Sort Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列 思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可 遍历一遍的话可以在遍历的同时分别与0和2比较,从头和尾一起交换,1的在中间不用做处理: * */ package javaTr

颜色排序--Sort Colors

https://leetcode.com/problems/sort-colors/ Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer

[LeetCode OJ] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer