恐慌:运行时错误:索引超出范围[-1]

huangapple go评论75阅读模式
英文:

panic: runtime error: index out of range [-1]

问题

当我尝试解决LeetCode上的118. 杨辉三角问题时,出现了奇怪的错误。下面的代码可以通过OJ测试:

func generate(numRows int) [][]int {
    res := [][]int{}
    for i := 0; i < numRows; i++ {
        row := []int{}
        for j := 0; j < i+1; j++ {
            if j == 0 || j == i {
                row = append(row, 1)
            } else if i > 1 {
                row = append(row, res[i-1][j-1] + res[i-1][j])
            }
        }
        res = append(res, row)
    }
    return res
}

但是我写的代码如下,却出现了panic错误,但基本逻辑是相同的:

func generate(numRows int) [][]int {
    res := [][]int{}
    for i := 0; i < numRows; i++ {
        row := []int{}
        for j := 0; j < i+1; j++ {
            if j == 0 || j == i {
                row = append(row, 1)
            }
            if i > 1 {
                row = append(row, res[i-1][j-1] + res[i-1][j])
            }
        }
        res = append(res, row)
    }
    return res
}

我使用了if else if结构,它运行良好,但是我使用了两个if条件判断错误。

实际上,它们的逻辑是相同的,但为什么会出错呢?如果您能解决这个问题,我将不胜感激。祝您好运!

英文:

when I try to solve the leetcode problem of 118. Pascal&#39;s Triangle https://leetcode.com/problems/pascals-triangle/

it occured strange bug. Below the code can pass OJ.

func generate(numRows int) [][]int {
	res := [][]int{}
	for i := 0; i &lt; numRows; i++ {
		row := []int{}
		for j := 0; j &lt; i+1; j++ {
			if j == 0 || j == i {
				row = append(row, 1)
			} else if i &gt; 1 {
				row = append(row, res[i-1][j-1] + res[i-1][j])
			}
		}
		res = append(res, row)
	}
	return res
}

But the code I write like this and it occur panic, but the basic logic is same.

func generate(numRows int) [][]int {
	res := [][]int{}
	for i := 0; i &lt; numRows; i++ {
		row := []int{}
		for j := 0; j &lt; i+1; j++ {
			if j == 0 || j == i {
				row = append(row, 1)
			}
            if i &gt; 1 {
				row = append(row, res[i-1][j-1] + res[i-1][j])
			}
		}
		res = append(res, row)
	}
	return res
}

I used if else if structure, it works fine, but I use 2 if condition judgment error.

In fact, their logic is the same, but why the error? I would be grateful if you could solve this problem. Good lcuk for you!

答案1

得分: 2

问题是在第二个版本中你使用了两个if条件,而在第一个版本中你使用了一个if else

但基本逻辑是相同的

不,逻辑是不同的。结果是当j为0时,你尝试执行j-1

如果你有两个像这样的if条件,程序将分别进入两个块,只要它们各自的条件满足。

if j == 0 || j == i {
    row = append(row, 1)
}
// 只要 i > 1,即使 j 是 0,你仍然会进入这个块
if i > 1 {
    row = append(row, res[i-1][j-1] + res[i-1][j])
}

如果第一个if条件满足,你可以使用continue跳过这部分。

if j == 0 || j == i {
    row = append(row, 1)
    // 继续下一次迭代
    continue
}
if i > 1 {
    row = append(row, res[i-1][j-1] + res[i-1][j])
}

也就是说,在你的代码的第一个版本中使用if else似乎是合理的。我不确定为什么你想要改变它。

英文:

The problem is that you use 2 if conditions in the second version, while in the first version you have an if else.

> but the basic logic is same

No, the logic is not the same. The result is that you try to do j-1 when j is 0.

If you have 2 if conditions like this, the program will enter both blocks individually if their respective condition is met.

if j == 0 || j == i {
    row = append(row, 1)
}
// if j is 0 you still enter this block as long as i &gt; 1
if i &gt; 1 {
    row = append(row, res[i-1][j-1] + res[i-1][j])
}

You could use continue to skip this section, if the first if is met.

if j == 0 || j == i {
    row = append(row, 1)
    // continue with the next iteration
    continue
}
if i &gt; 1 {
    row = append(row, res[i-1][j-1] + res[i-1][j])
}

That said, using the if else in the first version of your code seems reasonable. I am not sure why you would want to change it.

huangapple
  • 本文由 发表于 2022年4月10日 19:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/71816002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定