汉诺塔
public class Code_01_Hanoi {
public static void hanoi(int n){
if(n>0){
func(n,"左","右","中");
}
}
private static void func(int i, String start, String end, String other) {
if(i==1) {
System.out.println("Moving 1 from" + start + "to" + end);
}else{
func(i-1,start,other,end);
System.out.println("Moving "+i+"from "+start+"to "+end);
func(i-1,other,end,start);
}
}
}
全排列
/**
* @param str
* @param i
* @param res i之前的res列表
*/
public static void process(char[] str, int i, List<Character> res){
if(i == str.length){
printList(res);
return;
}
List<Character> resKeep = copyList(res);
resKeep.add(str[i]);
process(str,i+1,resKeep);
List<Character> resNoInclude = copyList(res);
process(str,i+1,resNoInclude);
}
private static List<Character> copyList(List<Character> res) {
return;
}
public static void printList(List<Character> res){
return;
}
``对上述代码的改进
/**
* @param str
* @param i
*/
public static void process(char[] str,int i){
if(i == str.length){
System.out.println(String.valueOf(str));
return;
}
process(str,i+1);//要当前字符
char tmp = str[i];
str[i] = 0;
process(str,i+1);//不要当前字符
str[i] = tmp;
}
全排列
public static void process(char[] str, int i, ArrayList<String> res){
if(i == str.length){
res.add(String.valueOf(str));
}
boolean[] visit = new boolean[26];
for(int j = i;j < str.length;j++){
if(!visit[str[j]-'a']){
visit[str[j]-'a']=true;
swap(str,i,j);
process(str,i+1,res);
swap(str,i,j);
}
}
}
private static void swap(char[] str, int i, int j) {
char tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
public static void main(String[] args) {
ArrayList<String> res = new ArrayList<>();
String ss = new String("abcd");
char[] str = ss.toCharArray();
process(str,0,res);
for(int i =0;i<res.size();i++){
System.out.println(res.get(i));
}
}
}
public static int win1(int[] arr){
if(arr == null || arr.length == 0) return 0;
return Math.max(f(arr,0,arr.length-1),s(arr,0,arr.length-1));
}
public static int f(int[] arr,int i,int j){
if(i == j) return arr[i];
return Math.max(arr[i]+s(arr,i+1,j),arr[j]+s(arr,i,j-1));
}
public static int s(int[] arr,int i,int j){
if(i == j) return arr[i];
return Math.min(arr[i]+f(arr,i+1,j),arr[j]+f(arr,i,j-1));
}
给你一个栈,逆序这个栈,不申请额外的数据结构,只能递归函数。
相当于把栈底的元素提上去
public static int f(Stack<Integer> stack){
int result = stack.pop();
if(!stack.isEmpty()){
return result;
}else{
int last = f(stack);
stack.push(result);
return last;
}
}
public static void reverse(Stack<Integer> stack){
if(!stack.isEmpty()) return;
int i = f(stack);
reverse(stack);
stack.push(i);
}
public static int process(char[] str,int i){
if(i == str.length) return 1;
if(str[i] == '0') return 0;
if(str[i] == '1'){
int res = process(str,i+1);
if(i + 1 < str.length){
res += process(str,i+2);
}
return res;
}
if(str[i] == '2'){
int res == process(str,i+1);
if(i+1 < str.length && (str[i+1] >= '0' && str[i+1]<='6')){
res += process(str,i+2);
}
return res;
}
//str[i] == '3'~'9'
return process(str,i+1);
}
下面第一种方法好,可变参数少
public static int process1(int[] weights,int[] values,
int i,int alreadyweight,int bag){
if(alreadyweight > bag) return 0;
if(i == weights.length) return 0;
return Math.max(
process1(weights,values,i+1,alreadyweight,bag),
values[i]+process1(weights, values, i+1, alreadyweight+weights[i], bag));
}
public static int process2(int[] weights,int[] values,
int i,int alreadyweight,int alreadyvalue,int bag){
if(alreadyweight > bag) return 0;
if(weights.length == i) return alreadyvalue;
return Math.max(
process2(weights,values,i+1,alreadyweight,alreadyvalue,bag),
process2(weights,values,i+1,alreadyweight+weights[i],values[i]+alreadyvalue,bag)
);
}