写一方法,List<String> create(String start,String end) ,规则如下
1,当start为a,end为af时,返回a,b,c……z,aa,ab,ac,……af,过渡时能从z到aa,zz到aaa,接着到aab,aac
2. 当如果start="af",end="ab",因为f比b大了。或者end.length()<start.length(),认为输入非法,return null;自己写了个,但是看着不顺,看看坛子里有无好的方法。
数据 System.out.println(create("a","z"));
System.out.println(create("w","ab"));//合法
System.out.println(create("zzx","aaad"));
System.out.println(create("af","ab"));
System.out.println(create("wwa","ab"));
System.out.println(create("ww","ww"));要求返回
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
[w, x, y, z, aa, ab]
[zzx, zzy, zzz, aaaa, aaab, aaac, aaad]
null
null
null

解决方案 »

  1.   

    import java.util.*;
    public class Test1{
    public static void main(String[] args){
    System.out.println(create("a","z")); 
    System.out.println(create("w","ab"));//合法 
    System.out.println(create("zzx","aaad")); 
    System.out.println(create("af","ab")); 
    System.out.println(create("wwa","ab")); 
    System.out.println(create("ww","ww")); 
    }
    public static List<String> create(String start,String end){
    if(start.length()>end.length()){
    return null;
    }
    if(start.length()==end.length()&&start.compareTo(end)>=0){
    return null;
    }
    List<String> myList=new ArrayList<String>();
    myList.add(start);
    char[] chars=start.toCharArray();
    while(!start.equals(end)){
    chars=add(chars,chars.length-1);
    start=new String(chars);
    //System.out.println(start);
    myList.add(start);
    }
    return myList;
    }

    public static char[] add(char[] chars,int x){
    if(x==0){
    if(chars[x]+1>'z'){
    char[] newChars=new char[chars.length+1];
    chars[x]='a';
    System.arraycopy(chars,0,newChars,1,chars.length);
    newChars[0]='a';
    return newChars;
    }else{
    chars[x]+=1;
    return chars;
    }
    }
    if(chars[x]+1>'z'){
    chars[x]='a';
    chars=add(chars,x-1);
    }else{
    chars[x]+=1;
    }
    return chars;
    }
         
    }
      

  2.   

    不知道为什么,让我想起了ACM的那些日子