A different Java approach


#1
public class Solution {
    public ArrayList<ArrayList<Integer>> diagonal(ArrayList<ArrayList<Integer>> A) {
        int i = 0, j = 0, row = 0, col = 0, l = 0;
        ArrayList<ArrayList<Integer>> B = new ArrayList<ArrayList<Integer>>();
        
        if(A.size() - 1 < 0 || A.get(0).size() - 1 < 0) return B;
        
        for(int k = 0; k < (A.get(0).size() * 2 - 1); k++){
            B.add(new ArrayList<Integer>());
        }
        while(true){
            ArrayList<Integer> C = B.get(l);
            C.add(A.get(i).get(j));
            i++;
            j--;
            if(j < 0 && i < A.size()){
                col++;
                j = col;
                i = row;
                l++;
            }
            if(i > A.size() - 1){
                row++;
                j = col;
                i = row;
                l++;
            }
            if(l == B.size()){
                break;
            }
        }
        
        return B;
    }
}