博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode75. Sort Colors
阅读量:7091 次
发布时间:2019-06-28

本文共 1994 字,大约阅读时间需要 6 分钟。

题目要求

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.    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?

假设一个数组中存在3个数字0,1,2。将数组中的数字排序,尽量实现O(n)时间复杂度。

思路一:O(2n)

就如题中所言,先遍历一遍数组,分别记录0,1,2出现的次数。然后在第二次遍历数组的过程中,将相应次数的0,1,2依序填入数组中。代码如下:

public void sortColors1(int[] nums) {        int numOfZero = 0;        int numOfOne = 0;        for(int i = 0 ; i < nums.length ; i++){            if(nums[i]==0){                numOfZero++;            }else if(nums[i]==1){                numOfOne++;            }        }        for(int i = 0 ; i
0){ nums[i] = 0; numOfZero--; }else if(numOfOne>0){ nums[i] = 1; numOfOne--; }else{ nums[i] = 2; } } }

思路二:双指针

如何能在一次遍历的过程中实现三个数字的排序呢~这就需要使用到双指针。我们要确保左指针之前的数值全部是0,右指针之后的数值全部是2。这样他就可以确保左右指针之间的数字为1,从而实现0,1,2的排序。代码如下:

private void swap(int[] nums, int i, int j) {        int tmp = nums[i];        nums[i] = nums[j];        nums[j] = tmp;    }        //保证i前全是0,j后全是2,然后通过交换使得i和j中间确定为1,O(n)    public void sortColors2(int[] nums) {        int i = 0;        int j = nums.length - 1;        int ptr = 0;        while (ptr <= j) {            if (nums[ptr] == 0) {                swap(nums, i++, ptr++);            } else if (nums[ptr] == 1) {                ptr++;            } else {                swap(nums, ptr, j--);            }        }    }

clipboard.png

想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

转载地址:http://sxiql.baihongyu.com/

你可能感兴趣的文章
云计算开始。。。
查看>>
利用sys.dm_db_index_physical_stats查看索引碎片等数据
查看>>
jquery html动态添加的元素绑定事件详解
查看>>
日常英语---九、MapleStory Link Skills Guide
查看>>
Hudson Script Console
查看>>
Android开发教程汇总
查看>>
最大流量dinci模板
查看>>
Linux Ubuntu搭建git服务器
查看>>
iOS - Swift Range 范围
查看>>
2017-2018-2偏微分方程复习题解析4
查看>>
【转】Python字符编码详解
查看>>
设置开机自启动VirtualBox虚拟机系统
查看>>
Wellner 自适应阈值二值化算法
查看>>
彻底理解PHP的SESSION机制
查看>>
HTTP Header 详解
查看>>
EasyUI效果--DataGrid的编辑效果
查看>>
IntelliJ IDEA---java的编译工具【转】
查看>>
Gradle用户指南(2)-基本命令
查看>>
python requests的安装与简单运用
查看>>
图像处理之opencv---常用函数
查看>>