简体   繁体   English

为什么 splice 会删除我的整个数组? 如果 max 在位置 0,我只想删除它

[英]Why is splice deleting my entire array? I would only like to delete max if it is in position 0

I'm working on a leet code problem with the following instructions:我正在使用以下说明处理 leet 代码问题:

You are given an array prices where prices[i] is the price of a given stock on the ith day.给定一个数组价格,其中价格 [i] 是给定股票在第 i 天的价格。

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.您希望通过选择一天购买一只股票并选择未来的另一天出售该股票来最大化您的利润。

Return the maximum profit you can achieve from this transaction.返回您可以从此交易中获得的最大利润。 If you cannot achieve any profit, return 0.如果您无法获得任何利润,则返回 0。

In my code below, I expect to remove the Max number from the array only if it's in index 0 of the array.在下面的代码中,我希望仅当它位于数组的索引 0 中时才从数组中删除 Max 数。 When checking the debugger, I see that the entire array is deleted except index 0. Why is splice not working as intended?检查调试器时,我看到除索引 0 之外的整个数组都被删除了。为什么 splice 没有按预期工作?

what debugger shows调试器显示什么leetcode 调试器视图显示整个数组已删除

var maxProfit = function (prices) {
  let theMin = Math.min(...prices)
  let minPosition = prices.indexOf(theMin)
  let theMax = Math.max(...prices)
  let maxPosition = prices.lastIndexOf(theMax)

  if (maxPosition === 0) {
    prices = prices.splice(0, 1)
    if (prices.length === 0) {
      return 0
    }

    maxProfit(prices)
  }

  return theMax - theMin
};

splice does not return a modified copy of the array. splice不返回数组的修改副本。 The array is modified in-place, and what is returned is the deleted subarray, if any.数组被就地修改,返回的是被删除的子数组(如果有的话)。

In effect, your code is:实际上,您的代码是:

const deletedElements = prices.splice(0, 1);
prices = deletedElements;

You deleted the first element from the array, then replaced that array with another array which only contains the first element.您从数组中删除了第一个元素,然后将该数组替换为另一个仅包含第一个元素的数组。 Thus, it is not that only the first element is returned, as you claim.因此,正如您所声称的那样,并不是只返回第一个元素。 splice is working exactly as documented. splice的工作方式与文档完全一致。


Also, prices.splice(0, 1) is more legibly written as prices.shift() .此外, prices.splice(0, 1)更清晰地写为prices.shift()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除对象属性是否像使用拼接删除数组元素一样? - Delete object properties similiar like deleting array elements with splice? 为什么我的接头会删除除第一个之外的所有接头,而不是仅删除指定的接头? - Why is my splice deleting all but the first instead of only the one specified? 删除JavaScript中的数组元素——删除vs拼接 - Deleting array elements in JavaScript - delete vs splice 只有一个参数的拼接不会删除整个数组 - splice with only one parameter not deleting the whole array 如何在P5JS中拼接2D数组或删除其中的元素? - How would I splice a 2D array in P5JS or delete an element within it? 我想使用 jQuery 同时从我的待办事项列表和 object 数组中删除一个项目 - I would like to delete an item off my todo list and my object array at the same time with jQuery 这只会删除表格中的第一行。 我希望它删除我使用jQuery&html单击的任何行 - This will only delete the first row in my table. I would like it to delete whichever row I click on using jQuery & html 为什么删除行方法会删除整个表? - Why Is Delete Rows Method Deleting an Entire Table? 为什么我的Delete函数只用Express删除Express中的第一个元素 - Why my Delete function deleting only First element in express with jquery 为什么 splice 不从我的数组中删除元素? - Why is splice not removing elements from my array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM