简体   繁体   English

当我增加用于从数组中获取元素的数字时会发生什么?

[英]What happens when I increment the number I'm using to fetch an element from an array?

I've been doing some algorithm questions and just saw a solution for a problem that is like this:我一直在做一些算法问题,只是看到了一个解决这样的问题的方法:

public int longestOnes(int[] nums, int k) {
  int windowStart = 0, windowEnd;
  for (windowEnd = 0; windowEnd < nums.length; ++windowEnd) {
    if (nums[windowEnd] == 0) k--;
    if (k < 0 && nums[windowStart++] == 0) k++;
  }
  return windowEnd - windowStart;
}

Specifically in the part where windowStart is incremented (nums[windowStart++]), I understand that it will first fetch the value from nums array using the current windowStart value and then it will be incremented.特别是在windowStart递增的部分(nums[windowStart++]),我知道它将首先使用当前windowStart值从nums数组中获取值,然后将其递增。

However, I don't understand exactly when this piece of code will be executed.但是,我不知道何时执行这段代码。 Only when k < 0?仅当 k < 0 时?

If so, is it correct to write this code like below:如果是这样,编写如下代码是否正确:

public int longestOnes(int[] nums, int k) {
  int windowStart = 0, windowEnd;
  for (windowEnd = 0; windowEnd < nums.length; ++windowEnd) {
    if (nums[windowEnd] == 0) k--;
    if (k < 0 && nums[windowStart] == 0) k++;
    if (k < 0) windowStart++;
  }
  return windowEnd - windowStart;
}

EDIT: I understand that in the third "if" k has been incremented and the condition is not gonna be the same.编辑:我知道在第三个“如果”中 k 已经增加并且条件不会相同。 I'm just trying to wrap my head around it by writing that second "if" in a different way.我只是想通过以不同的方式写第二个“如果”来绕过它。

Somehow it seems to give me different results.不知何故,它似乎给了我不同的结果。

Would anyone know the difference and what exactly happens during that second condition ( if (k < 0 && nums[windowStart] == 0) k++; )?有谁知道区别以及在第二种情况下到底发生了什么( if (k < 0 && nums[windowStart] == 0) k++; )?

Your rewrite produces different results because the second if you added is checking the new value of k , after it has been incremented in the previous line:您的重写会产生不同的结果,因为if您添加的第二个是检查k值,则在前一行中它已递增之后:

if (k < 0 && nums[windowStart] == 0) k++; // k is incremented here
if (k < 0) windowStart++; // then k is checked here

If k is -1 and nums[windowStart] == 0 .如果k-1nums[windowStart] == 0 k will change to 0 in the first line, and the k < 0 check in the second line will fail and windowStart++ will not run.第一行的k会变成 0,第二行的k < 0检查会失败, windowStart++不会运行。

In the original version, there is only one k < 0 check, and windowStart++ is run if that check is true.在原始版本中,只有一次k < 0检查,如果该检查为真,则运行windowStart++

If you want to rewrite the code in a way that doesn't put windowStart++ inside the array index, you can do:如果您想以不将windowStart++放入数组索引的方式重写代码,您可以执行以下操作:

if (k < 0) {
    if(nums[windowStart] == 0) {
        k++;
    }
    windowStart++;
}

The idea is that k < 0 is the condition on which we do windowStart++ , but the old value of windowStart before we increment it is used to access the array.这个想法是k < 0是我们执行windowStart++的条件,但是windowStart在我们递增之前的旧值用于访问数组。 And we only increment k if both k < 0 and nums[windowStart] == 0 are true.如果k < 0nums[windowStart] == 0都为真,我们只增加k

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM