第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given an array of n buckets, each containing a red, white, or blue pebble, sort them by color. The allowed operations are: swap(i,j): swap the pebble in buc
// 给定一个set字符和一个正数k,找出所有该做set它可以由长度构成k该字符串集合 /* Input: set[] = {'a', 'b'}, k = 3 Output: aaa aab aba abb baa bab bba bbb Input: set[] = {'a', 'b', 'c', 'd'}, k = 1 Output: a b c d package recursion; import java.util.ArrayList; public class N_sets_form
public class Solution { public void sortColors(int[] A) { int len=A.length; int beg=0; int end=len-1; int cur=0; while(cur<=end){ if(A[cur]==0) { swap(A,cur,beg); beg++; cur++; } else if(A[cur]==1) { cur++; } else { swap(A,cur,end); end--; } } } publ
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