[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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月23日 20时54分26秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
架构设计与分层
2019-04-26
【01】Java面试----基础方面的陷阱
2019-04-26
排序算法整合
2019-04-26
Java程序员常见笔试题分析
2019-04-26
Java笔试题
2019-04-26
Spring Boot快速入门---(一)spring boot的创建及几种启动方式
2019-04-26
【物联网实训项目】------(一)家庭智慧安防系统之前期项目工作准备
2019-04-26
【物联网实训项目】------(二)家庭智慧安防系统之定时监控
2019-04-26
【物联网实训项目】------(三)家庭智慧安防系统之实时监控
2019-04-26
【物联网实训项目】------(四)家庭智慧安防系统之智能温控
2019-04-26
【物联网实训项目】------(五)家庭智慧安防系统之智能监控
2019-04-26
【物联网实训项目】------(六)家庭智慧安防系统之智能监控
2019-04-26
【物联网实训项目】------(七)家庭智慧安防系统之人脸验证
2019-04-26
日常琐事(一)
2019-04-26
数据结构----绪论
2019-04-26
篇章二线性表---基础知识
2019-04-26
篇章二线性表---常见操作
2019-04-26