v2 time O(n) space O(n) Accepted

List<List<Integer>> r = new ArrayList<ArrayList<Integer>>(); is wrong!