递归实现一种字符串拼接方法

递归实现叫做weave的方法, 可以将两个字符串相拼接,如果一个字符串比另一个长,就会拼接完后直接继续显示之后的字符,如果有null, 则throw illegalArgumentException,如果是空字符,则返回空字符.
如图所示

img

强行递归呀,哈哈哈


public class Demo {
    public static void main(String[] args) {
        String a = "aa";
        String b = "bbb";
        String wa = wa(a, b);
        System.out.println(wa);
    }

    private static String wa(String a,String b){
        String aa = "";
        if (a.length() !=0){
            aa = a.substring(0,1);
            a = a.replaceFirst(aa,"");
        }
        String bb = "";
        if (b.length() !=0){
            bb = b.substring(0,1);
            b = b.replaceFirst(bb,"");
        }
        if ( aa.length() == 0 && bb.length() == 0){
            return "";
        }
        return aa + bb + wa(a,b);
    }
}

public class 字符串子序列 {
 
    public static void printAllSubString(String str){
        if(str == null){
            return;
        }
 
        char[] chars = str.toCharArray();
        if(chars.length > 0){
            String pre = new String("");   // pre:用于表示从0到i-1位置上形成的结果
            printAllSubString(0, pre, chars);
        }else{
            System.out.println("");     // 输入空字符串也会打印空
        }
    }
 
    public static void printAllSubString(int i, String pre, char[] chars){
        // 迭代终止条件
        if(i == chars.length){
            // 说明已经到最后一个字符了,所有的选择都已经做完了,应该返回了
            System.out.println(pre);
            return;
        }
 
        // 如果没有到最后一个字符,那么当前字符有两种选择:选择要 和 选择不要
        printAllSubString(i + 1, pre, chars);  // 不要当前字符
        printAllSubString(i + 1, pre + String.valueOf(chars[i]), chars);   // 要当前字符
    }
 
    // 测试
    public static void main(String[] args) {
        printAllSubString("abc");
    }
}

null异常,空字符串,长度不一字符串,各种情况都处理了

public class Test{
 
    public static void main(String args[]) {
        try {
            System.out.println("aaaa, bbbb结果:" + weave("", "aaaa", "bbbb", 0));
            System.out.println("hello, world结果:" + weave("", "hello", "world", 0));
            System.out.println("\"\", world结果:" + weave("", "", "world", 0));
            System.out.println("\"\", \"\"结果:" + weave("", "", "", 0));
            System.out.println("null, world结果:" + weave("", null, "world", 0));
        }catch(IllegalArgumentException iea) {
            System.out.println(iea.getMessage());
        }
    }
    
    public static String weave(String strNew, String strOne, String strTwo, int index){
        //进来先判断异常处理
        if(strOne == null || strTwo == null) {
            throw new IllegalArgumentException("有null字符串!");
        }
        //检查是否为""空字符串
        if(strOne.equals("")) {
            return strTwo;
        }
        if(strTwo.equals("")) {
            return strOne;
        }
        strNew = strNew + strOne.charAt(index);
        strNew = strNew + strTwo.charAt(index);
        if(index < strOne.length() - 1 && index < strTwo.length() - 1) {
            //递归
            return weave(strNew, strOne, strTwo, index + 1);
        }else {
            //判断是否有一个字符串没拼接完
            if(index == strOne.length() - 1 && index < strTwo.length() - 1) {
                //字符串1拼接完了,但字符串2没拼接完
                strNew = strNew + strTwo.substring(index, strTwo.length());
            }
            if(index < strOne.length() - 1 && index == strTwo.length() - 1) {
                //字符串2拼接完了,但字符串1没拼接完
                strNew = strNew + strOne.substring(index, strOne.length());
            }
            return strNew;
        }
    }
    
}

img

package com.example.demo003.controller;

public class Test01 {
    public static String wear(String str1, String str2) {
        if (str1 == null || str2 == null) {
            throw new IllegalArgumentException();
        }
        if ("".equals(str1) || "".equals(str2)) {
            return str1 + str2;
        }
        return _wear(str1, str2, 0);
    }

    public static String _wear(String str1, String str2, int count) {
        if (count == Math.min(str1.length(), str2.length()) - 1) {
            if (str1.length() > count) {
                return str1.substring(count);
            }
            if (str2.length() > count) {
                return str2.substring(count);
            }
        }
        return "" + str1.charAt(count) + str2.charAt(count) + _wear(str1, str2, ++count);
    }

    public static void main(String[] args) {
        System.out.println(wear("aaaa", "bbbb"));
        System.out.println(wear("hello", "world"));
        System.out.println(wear("recurse", "NOW"));
        System.out.println(wear("hello", ""));
        System.out.println(wear("", ""));
    }
}

    System.out.println(solution.weave("hello", "world"));

    public String weave(String s1, String s2) {
        if (s1 == null || s2 == null) throw new IllegalArgumentException();
        StringBuilder res = new StringBuilder();
        merge(s1, s2, 0, 0, res);
        return res.toString();
    }

    public void merge(String s1, String s2, int i1, int i2, StringBuilder res) {
        if (i1 >= s1.length() && i2 >= s2.length()) return;
        if (i1 < s1.length()) res.append(s1.charAt(i1));
        if (i2 < s2.length()) res.append(s2.charAt(i2));
        merge(s1, s2, i1 + 1, i2 + 1, res);
    }