class Solution {
public int[][] solution(int n) {
List<Integer[]> answer_list = new ArrayList<>();
hanoi(answer_list, n, 1,3,2);
int[][] answer = new int[answer_list.size()][2];
for(int i=0; i<answer.length; i++) {
Integer[] now = answer_list.get(i);
answer[i][0] = now[0];
answer[i][1] = now[1];
}
return answer;
}
void hanoi(List<Integer[]> answer_list, int n, int start, int to, int via){
if(n==1) {
move(answer_list, 1, start, to);
} else {
hanoi(answer_list, n–1, start, via, to);
move(answer_list, n, start, to);
hanoi(answer_list, n–1, via, to, start);
}
}
void move(List<Integer[]> answer_list, int n, int start, int to) {
Integer[] answer = {start, to};
answer_list.add(answer);
}
}
Earn big by sharing our offers—become an affiliate today! https://shorturl.fm/B2zLU
Join our affiliate program today and start earning up to 30% commission—sign up now! https://shorturl.fm/oTWDr
Unlock exclusive rewards with every referral—enroll now! https://shorturl.fm/xHtQ1
Refer friends and colleagues—get paid for every signup! https://shorturl.fm/vermg
Refer friends and colleagues—get paid for every signup! https://shorturl.fm/vermg
https://shorturl.fm/SnB9z
https://shorturl.fm/gUgcb
https://shorturl.fm/Y3vOR
https://shorturl.fm/Av3oR
https://shorturl.fm/M3M6C