package sort;
import java.util.ArrayList;
public class Test46 {
static int count = 0;
public static void main(String[] args) {
System.out.println(countString(12258));
}
public static int countString(int s) {
ArrayList<String> list = new ArrayList<String>();
function(s + "", 0, list);
return count;
}
private static void function(String string, int index,
ArrayList<String> list) {
// TODO Auto-generated method stub
if (index > string.length())// 递归出口,表示越界,直接return
return;
if (index == string.length()) {// 表示匹配成功,输出list中的字符串
System.out.println(list);
count++;
return;
}
String str = string.substring(index, index + 1);// 截取一个字符
if (Integer.parseInt(str) >= 0 && Integer.parseInt(str) <= 25) {
list.add(str);
} else {
return;
}
function(string, index + 1, list);// 在截取一个字符的基础上调用递归
list.remove(list.size() - 1);// 由于list是引用类型,需要删除添加的元素,防止对后面的操作产生影响
if (index + 2 <= string.length()) {
String str1 = string.substring(index, index + 2);// 截取两个字符
if (Integer.parseInt(str1) >= 0 && Integer.parseInt(str1) <= 25) {
list.add(str1);
} else {
return;
}
function(string, index + 2, list);// 在截取两个字符的基础上调用递归
list.remove(list.size() - 1);// 由于list是引用类型,需要删除添加的元素,防止对后面的操作产生影响
}
}
}