擅长:python、mysql、java
<p>问题来自:</p>
<pre><code>for i in heights:
if heights[j + 1] >= heights[j]:
j += 1
</code></pre>
<p>当您遍历<code>height</code>时,有时<code>j + 1 = len(height)</code>,然后<code>heights[j + 1]</code>超出范围。你知道吗</p>
<p>你应该这样循环:</p>
<pre><code>for i in range(len(heights) - 1):
if heights[j + 1] >= heights[j]:
j += 1
</code></pre>
<p>正如tobias所指出的,不再需要<code>j</code>,您可以使用<code>i</code>。你知道吗</p>
<pre><code>for i in range(len(heights) - 1):
if heights[i + 1] >= heights[i]:
return True
</code></pre>