Java面试题

1.已排序的两个字符串例[abc] [abcde]连接在一块输出成[aabbcde]

public class StringTest {
	public static String outString(String a,String b){
		char[] a1 = a.toCharArray();
		char[] b1 = b.toCharArray();
		StringBuffer sb = new StringBuffer();
		int aa=0;
		int bb=0;
		
		for(int i=aa;aa<a1.length;i++){
			for(int j=bb;bb<b1.length;j++){
				if(b1[j] <= a1[i]){ 
					sb.append(b1[j]);
					bb = j+1;
				}
				else {
					sb.append(a1[i]);
					aa = i+1;
					break;
				}
			}
			if(bb == b1.length){
				for(int k=aa;k<a1.length;k++) 
					sb.append(a1[k]);
				break;
			}
		}
		
		for(int k=bb;k<b1.length;k++) sb.append(b1[k]);
		System.out.println(sb.toString());
		return sb.toString();
	}
	public static void main(String[] args) {
		StringTest.outString("abcde","abc");
		//aabccddeeffgj
	}

}

你可能感兴趣的:(java,qq,面试,J#)