查找步行道距离之间的高度

发布于 2024-11-04 05:30:23 字数 1067 浏览 0 评论 0原文

我们有一系列高度,代表步行道沿线的高度。给定数组中的 start/end 索引,返回从 start 索引开始到 结束的遍历的变化总和>结束索引。例如,对于高度 {5, 3, 6, 7, 2}start=2end=4 的总和为1 + 5 = 6。 start end end 索引都是带有 start <= end 的数组的有效索引。

sumHeights({5, 3, 6, 7, 2}, 2, 4) => 6       
sumHeights({5, 3, 6, 7, 2}, 0, 1) => 2       
sumHeights({5, 3, 6, 7, 2}, 0, 4) => 11    

我正在努力解决这个问题,我已经尝试了其中的一部分,但我很困惑,并且得到了ArrayIndexOutOfBoundsException

 public int sumHeights(int[] heights, int start, int end) {        

   int total =0;   
   int difference =0;   
      for(int i=start;i<=end;i++){           
        if(heights[i] > heights[i++]){         
           difference =heights[i] - heights[i++];                  
        }else if(heights[i++] > heights[i]){         
           difference =heights[i++] - heights[i];            
        }   
        total+=difference;   
      }   
   return total;   
 }  

We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the sum of the changes for a walk beginning at the start index and ending at the end index. For example, with the heights {5, 3, 6, 7, 2} and start=2, end=4 yields a sum of 1 + 5 = 6. The start end end index will both be valid indexes into the array with start <= end.

sumHeights({5, 3, 6, 7, 2}, 2, 4) => 6       
sumHeights({5, 3, 6, 7, 2}, 0, 1) => 2       
sumHeights({5, 3, 6, 7, 2}, 0, 4) => 11    

i'm struggling to get this right, i have tried a part of this but im confused and im getting ArrayIndexOutOfBoundsException.

 public int sumHeights(int[] heights, int start, int end) {        

   int total =0;   
   int difference =0;   
      for(int i=start;i<=end;i++){           
        if(heights[i] > heights[i++]){         
           difference =heights[i] - heights[i++];                  
        }else if(heights[i++] > heights[i]){         
           difference =heights[i++] - heights[i];            
        }   
        total+=difference;   
      }   
   return total;   
 }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见你 2024-11-11 05:30:23

您在循环中增加 i ,因此可能会超出范围:

for(int i=start;i<=end;i++){      // here i might be last valid index, i.e. 4 in your example     
    if(heights[i] > heights[i++]){    //here i might be 5 (because of i++), i.e. you'd get the ArrayIndexOutOfBoundsException     
       difference =heights[i] - heights[i++]; //here i might be 6 (because of another i++), i.e. you'd get the ArrayIndexOutOfBoundsException                 
    }else if(heights[i++] > heights[i]){     //here i might be 5, i.e. you'd get the ArrayIndexOutOfBoundsException    
       difference =heights[i++] - heights[i];            
    }   
    total+=difference;   
  }   

要修复它,请使用:

for(int i=start;i<end;i++){   
int next = i + 1;     
    if(heights[i] > heights[next]){  
       difference =heights[i] - heights[next]; //assuming you mean next = i + 1 here and not next = i + 2 like in your code               
    }else if(heights[next] > heights[i]){    
       difference =heights[next] - heights[i];            
    }   
    else {
      difference = 0; //due to public demand I'll help you with this, too
    }

    total+=difference;   
  }   

编辑:您还可以使循环更简单:

for(int i=start;i<end;i++){   
  total += Math.abs(heights[i] - heights[i+1]);  
}   

You increment i in your loop thus possibly going out of bounds:

for(int i=start;i<=end;i++){      // here i might be last valid index, i.e. 4 in your example     
    if(heights[i] > heights[i++]){    //here i might be 5 (because of i++), i.e. you'd get the ArrayIndexOutOfBoundsException     
       difference =heights[i] - heights[i++]; //here i might be 6 (because of another i++), i.e. you'd get the ArrayIndexOutOfBoundsException                 
    }else if(heights[i++] > heights[i]){     //here i might be 5, i.e. you'd get the ArrayIndexOutOfBoundsException    
       difference =heights[i++] - heights[i];            
    }   
    total+=difference;   
  }   

To fix it, use:

for(int i=start;i<end;i++){   
int next = i + 1;     
    if(heights[i] > heights[next]){  
       difference =heights[i] - heights[next]; //assuming you mean next = i + 1 here and not next = i + 2 like in your code               
    }else if(heights[next] > heights[i]){    
       difference =heights[next] - heights[i];            
    }   
    else {
      difference = 0; //due to public demand I'll help you with this, too
    }

    total+=difference;   
  }   

Edit: you could also make the loop much simpler:

for(int i=start;i<end;i++){   
  total += Math.abs(heights[i] - heights[i+1]);  
}   
宫墨修音 2024-11-11 05:30:23

这是因为

  • 您在循环内递增了变量i。在循环内使用 i+1 而不是 i++++ 运算符不返回 i+1,而是返回 i 并赋值 i=i+1
  • 另一个问题是您的 for 循环对 i 的值有错误的定义。如果您想返回 sumHeights({11, 12}, 0, 1) 您只想运行循环一次,对吗?如果运行此循环两次,第二次运行时i+1 将等于2,并且会抛出索引越界异常。
  • 另一个问题是当 heights[i] == heights[i+1] 时 - 在这种情况下,不会重新计算差值,并且可能会在之前的循环运行中进行分配。您可以通过在循环内移动 difference 变量声明来解决它。

试试这个代码:

 public int sumHeights(int[] heights, int start, int end) {        

   int total =0;   
      for(int i=start;i<end;i++){           
        int difference =0;   
        if(heights[i] > heights[i+1]){         
           difference =heights[i] - heights[i+1];          
        }else if(heights[i+1] > heights[i]){         
           difference =heights[i+1] - heights[i];            
        }   
        total+=difference;   
      }   
   return total;   
 }

This is because

  • you increment the variable i inside the loop. Use i+1 instead of i++ inside the loop. The ++ operator does not return i+1 but rather returns i and assigns i=i+1.
  • Another problem is that your for loop has a wrong definition for the values of i. If you want to return sumHeights({11, 12}, 0, 1) you want to run the loop just once right? If you run this loop twice i+1 will be equal to 2 in the second run and it will throw the index out of bound exception.
  • another problem is when heights[i] == heights[i+1] - in that case difference is not re-calculated and may be assigned in a previous loop run. You can solve it by moving difference variable declaration inside the loop.

Try this code:

 public int sumHeights(int[] heights, int start, int end) {        

   int total =0;   
      for(int i=start;i<end;i++){           
        int difference =0;   
        if(heights[i] > heights[i+1]){         
           difference =heights[i] - heights[i+1];          
        }else if(heights[i+1] > heights[i]){         
           difference =heights[i+1] - heights[i];            
        }   
        total+=difference;   
      }   
   return total;   
 }
过度放纵 2024-11-11 05:30:23

我是这样做的:

 public int sumHeights(int[] heights, int start, int end) {
 int sum=0;
while(start<end)
 {
   sum+=Math.abs(heights[start+1]-heights[start]);
   start++;
 }
return sum;
}

I did it like this:

 public int sumHeights(int[] heights, int start, int end) {
 int sum=0;
while(start<end)
 {
   sum+=Math.abs(heights[start+1]-heights[start]);
   start++;
 }
return sum;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文