请实现一个函数打印最优移动轨迹。
给定一个 `int n` ,表示有 n 个圆盘。请返回一个 `string` 数组,其中的元素依次为每次移动的描述。描述格式为: `move from [left/mid/right] to [left/mid/right]`。
数据范围:
要求:时间复杂度 , 空间复杂度
2
["move from left to mid","move from left to right","move from mid to right"]
import java.util.*; public class Solution { static ArrayList<String> res = new ArrayList<>(); public ArrayList<String> getSolution (int n) { hanluo(n, "left", "mid", "right"); return res; } public void hanluo(int n, String left, String mid, String right) { if (n == 1) { res.add(String.format("move from %s to %s", left, right)); return; } hanluo(n - 1, left, right, mid); hanluo(1, left, mid, right); hanluo(n - 1, mid, left, right); } }
import java.util.*; public class Solution { public ArrayList<String> getSolution(int n) { ArrayList<String> list = new ArrayList<String>(); move(n,list,"left","mid","right"); return list; } public static void move(int n, ArrayList<String> list,String a,String b,String c){ if(n==1){ list.add("move from "+a+" to "+c); }else{ move(n-1,list,a,c,b); list.add("move from "+a+" to "+c); move(n-1,list,b,a,c); } } }
import java.util.*; public class Solution { public ArrayList<String> getSolution(int n) { ArrayList<String> list=new ArrayList<>(); tower(list,n,"left","mid","right"); return list; } public void tower(ArrayList<String> list,int n,String l,String m,String r){ if(n==1){ list.add("move from "+l+" to "+r); }else{ tower(list,n-1,l,r,m);//注意递归时的传值,不是直接写"left"、"right"、"mid" list.add("move from "+l+" to "+r); tower(list,n-1,m,l,r); } } }
import java.util.*; public class Solution { private ArrayList<String> ans = new ArrayList<>(); public ArrayList<String> getSolution(int n) { // write code here dfs(n, 1, 3); return ans; } public String getType(int x) { switch(x) { case 1: return "left"; case 2: return "mid"; case 3: return "right"; default: return "left"; } } public void dfs (int n, int begin, int end) { if (n == 0) { return; } int tmp = 2; if ((begin == 2 && end == 1 )|| (begin == 1 && end == 2)) { tmp = 3; } else if ((begin == 2 && end == 3 ) || (begin == 3 && end == 2)) { tmp = 1; } dfs(n-1, begin, tmp); String beginStr = getType(begin); String endStr = getType(end); ans.add("move from " + beginStr + " to " + endStr); dfs(n-1, tmp, end); } }
import java.util.*; public class Solution { ArrayList<String> result = new ArrayList<>(); public ArrayList<String> getSolution(int n) { // write code here hanoi(n, "left", "mid", "right"); return result; } private void hanoi(int n, String from, String by, String to) { if (n == 0) { return; } if (n == 1) { result.add("move from " + from + " to " + to); // 一个的时候直接转移 } else { hanoi(n - 1, from, to, by); // 先借助第三根柱子转移到第二根柱子 result.add("move from " + from + " to " + to); hanoi(n - 1, by, from, to); // 再从第二根柱子借助地一根柱子转移到第三根柱子 } } }
import java.util.*; public class Solution { ArrayList<String> result = new ArrayList<>(); public ArrayList<String> getSolution(int n) { // write code here hanoi(n, "left", "mid", "right"); return result; } private void hanoi(int n, String from, String by, String to) { if (n == 0) { return; } hanoi(n - 1, from, to, by); // 先借助第三根柱子转移到第二根柱子 result.add("move from " + from + " to " + to); hanoi(n - 1, by, from, to); // 再从第二根柱子借助地一根柱子转移到第三根柱子 } }
import java.util.*; public class Solution { ArrayList<String> str=new ArrayList<>(); public ArrayList<String> getSolution(int n) { // write code here HanNuo(n,"left","mid","right"); return str; } public void HanNuo(int n,String left,String mid,String right){ if(n==1){ str.add("move from "+left+" to "+right); } else{ HanNuo(n-1,left,right,mid); str.add("move from "+left+" to "+right); HanNuo(n-1,mid,left,right); } } }
import java.util.*; public class Solution { public static ArrayList<String> getSolution(int n) { // write code here ArrayList<String> result=new ArrayList<>(); if(n==0) {} else { Solution(result,n, "left", "right", "mid"); } return result; } public static void Solution(ArrayList<String> array, int n, String x,String y,String z) { if(n==0) {} else { Solution(array,n-1, x, z, y); array.add("move from "+x+" to " +y); Solution(array,n-1, z, y, x); } } }
import java.util.*; public class Solution { public void solution(int n,String begin,String end,String tmp,ArrayList<String> res){ if(n<=0) return ; if(n==1) res.add(new String("move from "+begin+" to "+end)); else{ solution(n-1,begin,tmp,end,res); solution(1,begin,end,tmp,res); solution(n-1,tmp,end,begin,res); } } public ArrayList<String> getSolution(int n) { ArrayList<String> res=new ArrayList<String>(); solution(n,"left","right","mid",res); return res; } }
import java.util.*; public class Solution { public ArrayList<String> getSolution(int n) { ArrayList<String> list = new ArrayList<>(); hanoi(list, n, "left", "mid", "right"); return list; } public static void hanoi(ArrayList<String> list, int n, String from, String denpend, String to) { if(n == 1) { list.add("move from " + from + " to " + to); } else { hanoi(list, n - 1, from, to, denpend); hanoi(list, 1, from, denpend, to); hanoi(list, n - 1, denpend, from, to); } } }
import java.util.*; public class Solution { private String[] pos = {"left", "mid", "right"}; public ArrayList<String> getSolution(int n) { return getSolution(n, 0, 2); } public ArrayList<String> getSolution(int n, int from, int to) { if(n==0) return new ArrayList<String>(); ArrayList<String> list = getSolution(n-1, from, 3-from-to); list.add("move from "+pos[from]+" to "+pos[to]); list.addAll(getSolution(n-1, 3-from-to, to)); return list; } }
import java.util.*; public class Solution { public void move(int n, ArrayList<String> list, String a, String b, String c) { if (n == 1) { list.add(String.format("move from %s to %s", a, b)); } else { move(n - 1, list, a, c, b); move(1, list, a, b, c); move(n - 1, list, c, b, a); } } public ArrayList<String> getSolution(int n) { // write code here ArrayList<String> list = new ArrayList<>(); move(n, list, "left", "right", "mid"); return list; } }