[LeetCode]Sort Colors
发布日期:2021-11-22 02:48:48 浏览次数:2 分类:技术文章

本文共 622 字,大约阅读时间需要 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.

题意:将一堆乱序的0,1,2组成的数组将其他排成0,1,2有序的

题解:采用2个指针,start记录0前面的数为止,end为2的为止,遍历数组遇到0,与前面的数交换,遇到2与后面的数交换。

code:

package com.leetcode.mair;public class Solution75{		public void sortColors(int[] nums) {    		int start=0,end = nums.length-1;				if(nums.length<2)			return;				for(int i=0; i

转载地址:https://blog.csdn.net/zxdfc/article/details/48443333 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring 自动扫描组件
下一篇:Spring Collections(List,Set,Map,Properties)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月23日 20时54分26秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章