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

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

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?

two-pass solution

1 public class Solution { 2     public void sortColors(int[] A) { 3         // Start typing your Java solution below 4         // DO NOT write main() function 5         int len = A.length; 6         Map
times = new HashMap
(); 7 for(int i = 0; i < len; i++){ 8 if(times.get(A[i]) != null){ 9 times.put(A[i], times.get(A[i]) + 1);10 } else {11 times.put(A[i], 1);12 }13 }14 15 //A = new int[len];16 int red = 0, white = 0, blue = 0;17 18 if(times.get(0) != null)19 red = times.get(0);20 21 if(times.get(1) != null)22 white = times.get(1);23 24 if(times.get(2) != null)25 blue = times.get(2);26 27 for(int i = 0; i < len; i++){28 if(i < red){29 A[i] = 0;30 } else if(i >= red && i < red + white){31 A[i] = 1;32 } else {33 A[i] = 2;34 }35 }36 }37 }

 one pass

维护两个指针:redIdx,blueIdx,从头开始扫描数组直到blueIdx(包括blueIdx)

1.A[i]==0时,将A[i]与A[redIdx]交换,redIdx++,i++

2.A[i]==2时,将A[i]与A[blueIdx]交换,blueIdx--,

3.A[i]==1时,i++

1 public class Solution { 2     public void sortColors(int[] A) { 3         // Start typing your Java solution below 4         // DO NOT write main() function 5         int len = A.length; 6         int i = 0, redIdx = 0, blueIdx = len - 1; 7         while(i < blueIdx + 1){ 8             if(A[i] == 0){ 9                 swap(A, i, redIdx);10                 redIdx ++;11                 i ++;12             } else if(A[i] == 2){13                 swap(A, i, blueIdx);14                 blueIdx --;15             } else{16                 i++;17             }18         }19     }20     21     public void swap(int[] A, int i, int idx){22         int tmp = A[i];23         A[i] = A[idx];24         A[idx] = tmp;25     }26 }

 

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

你可能感兴趣的文章
智能家居——IoT零基础入门篇
查看>>
《Linux From Scratch》第一部分:介绍 第一章:介绍-1.3. 更新日志
查看>>
阿里将在雄安新区设3家子公司:涉AI、蚂蚁金服和菜鸟;北航设立全国首个人工智能专业,与百度合作办学...
查看>>
Powershell指令集_2
查看>>
归并排序算法
查看>>
北京第一个公共云计算平台即将诞生
查看>>
5G频谱相争“兵戎相见”各相部署风起云涌
查看>>
安全自动化在于信任,而非技术
查看>>
缘何Square可以在移动支付领域上成功?
查看>>
云计算从“仰望星空”到“脚踏实地”
查看>>
台积电要造第一款7nm芯片 明年下半年可投产
查看>>
《逻辑与计算机设计基础(原书第5版)》——3.9 二进制加法器
查看>>
《中国人工智能学会通讯》——8.25 基于演化优化的生物网络配准
查看>>
飞鹤乳业CIO:移动化让企业品牌和消费者紧密连接
查看>>
当精准广告遇到大数据
查看>>
《机器人自动化:建模、仿真与控制》——2.3 仿真
查看>>
泰一指尚大数据应用成为第一批省级重点企业研究院
查看>>
预测未来 盘点大数据分析领域五大趋势
查看>>
教你编写Node.js中间件,实现服务端缓存
查看>>
又到中元节 应用宝教你如何打败各种鬼
查看>>