Leetcode:214. Shortest Palindrome

解法一:很奇妙的解法:https://www.programcreek.com/2014/06/leetcode-shortest-palindrome-java/

public String shortestPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while (j >= 0) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;
            }
            j--;
        }

        if(i==s.length()){     //递归调用的终止条件
            return s;
        }
        String suffix = s.substring(i);
        String prefixString = new StringBuffer(suffix).reverse().toString();
        String mid = shortestPalindrome(s.substring(0, i));   //mid调用了shortestPalindrome(),使得中间部分返回的是回文字符串
return prefixString + mid + suffix; }
原文地址:https://www.cnblogs.com/Michael2397/p/8041684.html