October 22, 2024
Chicago 12, Melborne City, USA
java

What's wrong with the Tower of Hanoi


import java.util.ArrayList;
import java.util.List;

public class Solution {


    public void move(int size, List<Integer>A, List<Integer>B, List<Integer>C){
        if (size == 1){
            C.add(A.get(0));
            A.remove(0);
        }
        else {
            move(size-1,A,C,B);
            C.add(A.get(0));
            A.remove(0);
            move(size-1,B,A,C);
        }
    }

    public static void main(String[] args){
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();
        List<Integer> c = new ArrayList<>();
        a.add(3);
        a.add(2);
        a.add(1);
        a.add(0);

       Solution solution = new Solution();
       solution.move(a.size(),a,b,c);
    }
}

I just can’t find anything wrong with it.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video