Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

合并两个有序数组——题解Bug #29

Open
yyxTheOracle opened this issue Jun 23, 2022 · 0 comments
Open

合并两个有序数组——题解Bug #29

yyxTheOracle opened this issue Jun 23, 2022 · 0 comments

Comments

@yyxTheOracle
Copy link

您的题解链接

Bug复现

输入数据为 [0], 0, [1], 1,预期结果为 [1] ,实际结果为 [undefined]。

原因

41-45行,在处理A数组为空的情况时,遗漏了return。

    if (!A.length || !m) {
        for (let i = 0; i < n; i++) {
            A[i] = B.shift()
        }
        return // 此处应return
    }

否则继续往下执行至58-63行时,由于43行的shift方法已经将B数组的全部元素弹出,B[bIndex]成为了undefined,造成结果错误。

// 数组 A 遍历完,B 还有
    if (aIndex < 0) {
        while (bIndex >= 0) {
            A[mIndex--] = B[bIndex--] // 此时的 B[bIndex] 为undefined!!
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant