标签:
给定字符串,返回最少插入多少个字符,才能使该字符串变成回文字符串
public static int test(String str) { int len = str.length(); int ret = 0; int i = 0; int j = len - 1; if (j > i) { char c1 = str.charAt(i); char c2 = str.charAt(j); if (c1 == c2) { String substr = str.substring(i + 1, j); ret = test(substr); } else { String substr1 = str.substring(i, j); String substr2 = str.substring(i + 1, j + 1); int r1 = test(substr1); int r2 = test(substr2); ret = Math.min(r1, r2) + 1; } } return ret; }
标签:
原文地址:http://www.cnblogs.com/fyzjhh/p/5502317.html