1. 程式人生 > >字串反轉的兩種方式

字串反轉的兩種方式

public static  String charAtReverse(String s){
    int a = s.length();
    String reverse = "";
    for(int i=0;i<a;i++){
        reverse = s.charAt(i) +reverse;
    }
    return reverse;
}
public static  String charAtReverseSub(String s){
    int a = s.length();
    if(a<=1){
        return s;
    }
    String left = s.substring(0,a/2);
    String right = s.substring(a/2,a);
    String reverseSub = charAtReverseSub(right)
            + charAtReverseSub(left);
    return reverseSub;
}