面试过程中没有想到合适的数据结构,想了半天,构思没有实践落地。
也是听错了,以为三道题都要做,做出来了两道,这道死活没落地成功。
结果,三道题里面任选一道就行。。。。
好吧
题目:给定一个数字矩阵,按照顺时针,螺旋输出。
举例如图:
代码如下:
public static void spiralOutput(){
LinkedList<LinkedList> outList = new LinkedList();
LinkedList innerList1 = new LinkedList();
innerList1.add(1);
innerList1.add(2);
innerList1.add(3);
innerList1.add(4);
LinkedList innerList2 = new LinkedList();
innerList2.add(5);
innerList2.add(6);
innerList2.add(7);
innerList2.add(8);
LinkedList innerList3 = new LinkedList();
innerList3.add(9);
innerList3.add(10);
innerList3.add(11);
innerList3.add(12);
LinkedList innerList4 = new LinkedList();
innerList4.add(13);
innerList4.add(14);
innerList4.add(15);
innerList4.add(16);
outList.add(innerList1);
outList.add(innerList2);
outList.add(innerList3);
outList.add(innerList4);
System.out.println(outList);
while (outList.iterator().hasNext()) {
Iterator innerFirstIt = outList.getFirst().iterator();
while (innerFirstIt.hasNext()) {
System.out.println(innerFirstIt.next());
}
outList.removeFirst();
for (int i = 0; i <= outList.size() - 1; i++) {
System.out.println(outList.get(i).getLast());
outList.get(i).removeLast();
}
LinkedList lastList = outList.getLast();
for (int i = 0; i <= lastList.size() - 1; i++) {
System.out.println(lastList.get(lastList.size() - 1 - i));
}
outList.removeLast();
for (int i = 0; i <= outList.size() - 1; i++) {
LinkedList innerList = outList.get(outList.size() - 1 - i);
System.out.println(innerList.getFirst());
innerList.removeFirst();
}
}
}