列表

详情


FED60. 删除数组最后一个元素

描述

删除数组 arr 最后一个元素。不要直接修改数组 arr,结果返回新的数组

示例1

输入:

[1, 2, 3, 4]

输出:

[1, 2, 3]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 855ms, 内存消耗: 77824KB, 提交时间: 2020-11-23

{"css":"","js":"function truncate(arr) {\nlet arr2 = []\n  for (let i = 0; i < arr.length; i++) {\n    arr2.push(arr[i])\n  }\n  arr2.splice(arr2.length - 1, 1)\n  return arr2\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77812KB, 提交时间: 2020-12-26

{"css":"","js":"function truncate(arr) {\n var su = [];\n    for(var i = 0; i < arr.length - 1; i++){\n        su.push(arr[i]);\n    }\n    return su;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77824KB, 提交时间: 2020-11-22

{"css":"","js":"function truncate(arr) {\n let newArr=[]\n for(let i=0;i<arr.length-1;i++){\n    newArr.push(arr[i])\n}\n    return newArr\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77860KB, 提交时间: 2020-11-22

{"css":"","js":"function truncate(arr) {\n    let result =[]\n    for(let i=0; i<arr.length-1; i++){\n        result.push(arr[i])\n    }\n    return result\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 858ms, 内存消耗: 77868KB, 提交时间: 2020-12-25

{"css":"","js":"function truncate(arr) {\n    var newarr=arr.slice(0);\n    newarr.pop();\n    return newarr;\n}","html":"","libs":[]}

上一题