1. zigzag print一个matrix,input: [ [1, 2, 3], [4, 5, 6] ]
output: [ [1], [2, 4], [3, 5], [6] ]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public void zigzagPrint(List<List<Integer>> input) {
int index = 0;
boolean print = true;
while (print) {
print = false;
int tmpIndex = index;
for (List<Integer> list : input) {
if (tmpIndex < list.size()) {
print = true;
System.out.print(list.get(tmpIndex));
}
if (tmpIndex == 0) break;
tmpIndex--;
}
if (print)
System.out.println();
index++;
}
}
public static void main(String args[]) {
Solution solution = new Solution();
List<List<Integer>> input = new ArrayList<>();
input.add(Arrays.asList(1, 2, 3));
input.add(Arrays.asList(4, 5, 6));
solution.zigzagPrint(input);
}
}
没有评论:
发表评论