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

8.1.1 初始化大顶堆比较结果可能溢出 #1537

Closed
littletoxic opened this issue Oct 28, 2024 · 1 comment
Closed

8.1.1 初始化大顶堆比较结果可能溢出 #1537

littletoxic opened this issue Oct 28, 2024 · 1 comment

Comments

@littletoxic
Copy link

C# 版本中,如果 y - x 溢出会返回错误结果

// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));

可以使用自带的 CompareTo 方法

PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));

或是更改比较器实现

PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => {
    if (y < x)
        return -1;
    if (y > x)
        return 1;
    return 0;
}));
@hpstory
Copy link
Contributor

hpstory commented Oct 31, 2024

Hi @littletoxic , 感谢指出,会通过PR #1542 修改。

@krahets krahets closed this as completed Oct 31, 2024
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

3 participants