var arr = [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ]; function outputArr(arr){ var n = arr.length, newArr = []; for (var i = n - 1; i >= 0; i--) { //右上角的三角形 var temp = 0; for (var j = i; j < n; j++) { newArr.push(arr[temp++][j]); } } for (var i = 1; i < n; i++) { //左下角的三角形 var temp = 0; for (var j = i; j < n; j++) { newArr.push(arr[j][temp++]); } } return newArr; } console.log(outputArr(arr)); //[4, 3, 8, 2, 7, 12, 1, 6, 11, 16, 5, 10, 15, 9, 14, 13]
时间: 2024-10-10 17:42:32