Multiple left rotations of the array

Given an array of integers A and multiple values in B which represents the indices of the array A 
around which left rotation of the array A needs to be performed.

Find the rotated array for each value and return the result in the from of a matrix where i’th row represents the rotated
array for the i’th value in B.



Input Format

The first argument given is the integer array A.
The second argument given is the integer array B.

Output Format

Return the resultant matrix.

Constraints

1 <= length of both arrays <= 2000
-10^9 <= A[i] <= 10^9 
0 <= B[i] <= 2000

For Example

Input 1:
    A = [1, 2, 3, 4, 5]
    B = [2, 3]
Output 1:
    [ [3, 4, 5, 1, 2]
      [4, 5, 1, 2, 3] ]

Input 2:
    A = [5, 17, 100, 11]
    B = [1]
Output 2:
    [ [17, 100, 11, 5] ]
思路:

就按照题目要求做就可以了。注意B里的数有可能超过A的size,所以要mod一下A的size。

Solution:

Time: O(A * B)
Space: O(A * B)

public class Solution {
    public ArrayList<ArrayList<Integer>> solve(ArrayList<Integer> A, ArrayList<Integer> B) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();
        for (int i = 0; i < B.size(); i ++) {
            ArrayList<Integer> row = new ArrayList<>();
            int pivot = B.get(i) % A.size();
            row.addAll(A.subList(pivot, A.size()));
            row.addAll(A.subList(0, pivot));
            result.add(row);
        }
        return result;
    }
}