rearrange array (red, white, blue)
来源:互联网 发布:js 获取div下所有的li 编辑:程序博客网 时间:2024/05/15 13:40
1.Description
Suppose an array
2.Algorithm
Partition the array using Red as pivot. When done, all the red element will be at the beginning of the array.
Partition the remaining array, from the last Red element until the end, using Blue as pivot. Done.
3.Implementation
import java.util.Arrays;public class HW2 {public static void rearrange(String a[]){int i=0,n=a.length,j=n-1;while(i<j){while(i<n&&a[i]=="red") i++;while(j>=0&&a[j]!="red") j--;if(i<j) swap(a,i,j);}j=n-1;while(i<j){while(i<n&&a[i]=="white") i++;while(j>=0&&a[j]!="white")j--;if(i<j) swap(a,i,j);}}//exchange two elements in the arraypublic static void swap(String a[] ,int i,int j){String temp=a[i];a[i]=a[j];a[j]=temp;}public static void main(String[] args){String seq[] = {"blue", "white","red", "blue", "blue", "red", "red", "white", "red","white"};rearrange(seq);System.out.println(Arrays.toString(seq));}}
Screenshot of the result:
- rearrange array (red, white, blue)
- [Google]Rearrange White Spaces
- hdu4263 Red/Blue Spanning Tree
- hdu4263 Red/Blue Spanning Tree
- [agc14e]Blue and Red Tree
- matlab Permute Rearrange dimensions of N-D array
- Rearrange an array of positive and negative integers
- CareerCup Rearrange an array using swap with 0
- White Paper: Red Hat Crash Utility
- codeforces755E PolandBall and White-Red graph -- 构造
- 【图论】【匹配】pku3715 Blue and Red
- hdu 4263 Red/Blue Spanning Tree
- code forces 399B Red&Blue Balls
- codeforces#233_div2_B Red and Blue Balls
- POJ 3715 Blue and Red 二分图
- [HDU 4263]Red/Blue Spanning Tree[kruskal]
- hdu 4263 Red/Blue Spanning Tree
- Codeforces 399B Red & Blue Balls [数论]
- Patch android wifi to enable IBSS/WEP support
- MTU 检测和设置
- web漏洞详解及修复建议--阿里云
- CFI Flash, JEDEC Flash ,Parellel Flash, SPI Flash, Nand Flash,Nor Flash的区别和联系
- 【转】学习openstack云第一天
- rearrange array (red, white, blue)
- U-Boot操作Flash (NOR, NAND & SPI)
- 进程同步的几种机制
- oracle\EBS\常用表\视图\会计分录\mtl_serial_numbers\总账
- windows8中的布局问题
- MyBatis批量大数据测试的一些结果
- MySQL数据库设计需要注意的点
- 找出一堆硬币中较重的一个(java 算法)
- 窗口管理