写一方法,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,当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
解决方案 »
- 类File的问题,求助
- 用JMagick处理gif动画图片,把图象缩小后,怎么才能保留原有的动画效果,谢谢!
- java,javax,com.sun,org.omg的区别。。
- 奇怪的nullpointerexception,急!!帮忙看看,帮顶也有分!!!
- 急求!!<FORM id=FORM1 name=fm_login action="j_security_check" method=post>
- 有关SQL???
- 哪位高手用过IBM MQ, 有什么看法?
- 一个很可笑的问题,System.in怎么用,立刻给分
- java两个接口,合写成一个????
- 浅谈java线程定时任务timerTask
- java聊天室,server能获得信息,实在是找不到问题了.....
- 如和将dom4j-1.6.zip整合到MyEclipse6.0中?
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;
}
}