leetcode:Sort Colors

一、     题目

给一个数组包含n个物体,有蓝色、红色和白色三种颜色,把他们分类并按照红、白、蓝的顺序排列,我们用0、1、2来表示红白蓝的颜色

注解:很容易想到遍历两遍数组得到三个数的数目,再覆盖,但是请只遍历一遍数组来解决。

二、     分析

很简单,题目的意思其实就是让对一个数组排序,数组中的元素只有0、1、2,并且要求只能遍历一遍数组,常数空间复杂度。此时我们可以想到借助于快速排序的partition思想,以1为中间枢纽对数组进行划分,使0在数组的左边,2在数组的右边,1在数组的中间。

class Solution {
public:
    void sortColors(int A[], int n) {
        int end = -1;
		int start = n;
		int i=0;
		//end是放0那部分的尾部索引,start是放2那部分的首部索引
        //碰到0放到end+1处,碰到2放到start-1处,碰到1指针直接后移
		while(i<start){
		  if(A[i] == 0 && i!=++end)
			 swap(A[end],A[i]);
		  else if(A[i] == 2 && i!= --start)
			 swap(A[start],A[i]);
		  else
			 i++;
		}
    }
};
时间: 2024-11-03 22:25:38

leetcode:Sort Colors的相关文章

【LeetCode】Sort Colors

LeetCode OJ 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, w

[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 数组排序

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

Leetcode:Sort List

题目:Sort List Sort a linked list in O(n log n) time using constant space complexity 看题目有两个要求:1)时间复杂度为O(nlogn);2)空间复杂度为常数,即不能增设额外的空间.满足这样要求的排序算法,我们首先想到快排,合并排序和堆排序.我们来分析下几种排序算法对时间和空间复杂度的要求,堆排序实现上过于繁琐,我们不做考虑.快排的最坏的时间复杂度是O(n^2),平均复杂度为O(nlgn),如果按照题目的严格要求显然

leetcode笔记: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

[leetcode 75] Sort Colors

1 题目 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, a

LeetCode 75. 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 No75. Sort Colors

Question: 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, whi

leetcode 【 Sort Colors 】python 实现

题目: 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, an