思想:这个思想就是先推,再测试,我们假设n个字符,那么最多交换1+2+…+n-1次,即(n-1)*n/2次。若交换100次,n-1)*n/2=100, n = 15, 最少使用15个字母才可以完成,考虑字典序最小,就使用前15个字母,就是前15个字母逆序,并将第六个字符移到第一位。
即:jonmlkihgfedcba
然后写个冒泡排序检验以下是不是移动100次即可
/**
* 排序
* 字符串排序,交换100次,字典序最小的仅含有小写字母且不含重复元素的字符串
*/
public class Main {
public static int f(char [] arr){
int count = 0 ;
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr.length-i-1; j++){
if(arr[j] > arr[j+1]){
char temp = arr[j] ;
arr[j] = arr[j+1] ;
arr[j+1] = temp ;
count++ ;
}
}
}
return count ;
}
public static void main(String[] args) {
String s = "jonmlkihgfedcba" ;
System.out.println(f(s.toCharArray()));
}
}