我的IEquatable <t>,IComparable <t>实现有什么问题? SortedList引发ArgumentException

时间:2018-12-13 06:00:08

标签: c# .net icomparable iequatable

我正在在线解决一个难题,偶然发现了这个问题,给定一个2D矩阵和一个数字k,我需要返回矩阵中第k个最小的元素。

 matrix = [
       [ 1,  5,  9],
       [10, 11, 13],
       [12, 13, 15]
    ],
    k = 8,

return 13.

我可以通过自己的二进制堆实现解决此问题。由于我正在准备面试,因此不确定在所有情况下实施自己的堆是否可以接受。所以我试图用SortedList / SortedSet解决这个问题。

我基本上是在创建一个Point对象,该对象接受索引i,j和i,j处的值。我已经实现了IComparable和IEquatable。但是由于某些原因,在上面的示例中,索引1,2(值13)的Point对象和索引2,1(值13)的Point对象在不应该相等时被视为相等。使用SortedList时,我得到一个ArgumentException,同时SortedSet只是覆盖了现有对象。我的IEquatable,IComparable实现是否错误?我仔细检查了它们是否生成了不同的哈希码。

P.S。这不是作业问题。我正在通过在线面试准备平台解决问题。

public class Solution {
    public int KthSmallest(int[,] matrix, int k) {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        SortedSet<Point> pq = new SortedSet<Point>();
        bool[,] visited = new bool[rows, cols];
        int count = 1;
        int i=0, j=0;
        var start = new Point(i, j, matrix[i, j]);
        pq.Add(start);
        visited[0, 0] = true;
        while(true) {
            k--;
            var curr = pq.Min;
            pq.Remove(pq.First());
            if(k == 0)
                return curr.val;
            i = curr.x + 1;
            j = curr.y;
            if(i < rows && j < cols && !visited[i, j]) {
                var next = new Point(i, j, matrix[i, j]);
                Console.WriteLine(next.GetHashCode());
                Console.WriteLine(i+", "+j+", "+next.val);
                pq.Add(next);
                visited[i, j] = true;
            }
            i = curr.x;
            j = curr.y + 1;
            if(i < rows && j < cols && !visited[i, j]) {
                var next = new Point(i, j, matrix[i, j]);
                Console.WriteLine(next.GetHashCode());
                Console.WriteLine(i+", "+j+", "+next.val);
                pq.Add(next);
                visited[i, j] = true;
            }
        }
    }
}


public class Point : IComparable<Point>, IEquatable<Point>
{
    public int x { get; private set; }
    public int y { get; private set; }
    public int val { get; private set; }

    public Point(int x, int y, int val)
    {
        this.x = x;
        this.y = y;
        this.val = val;
    }

    public int CompareTo(Point that)
    {
        if(this.val == that.val) {
            if(this.x == that.x) {
                return this.y.CompareTo(that.y);
            }
            else {
                this.x.CompareTo(that.x);
            }
        }
        return val.CompareTo(that.val);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((Point)obj);
    }

    public bool Equals(Point p1) {
        return x == p1.x && y == p1.y && val == p1.val;
    }

    public override int GetHashCode() {
        long hashCode = ((x * 31 + y) * 31 + val);
        return hashCode.GetHashCode();
    }
}

1 个答案:

答案 0 :(得分:1)

您在CompareTo中缺少return语句。我在下面评论了您的原始照片,以及更正的版本。在比较[2,1]和[1,2]时,x值不匹配,但是当您点击this.x.CompareTo时,您实际上不会返回该比较,因此将返回值比较。

您有:

public int CompareTo(Point that)
    {
        if(this.val == that.val) {
            if(this.x == that.x) {
                return this.y.CompareTo(that.y);
            }
            else {
                //****MISSING RETURN STATEMENT - 
                //will return the val.ComapreTo statement after 
                //it leaves this block*****
                this.x.CompareTo(that.x);
            }
        }
        return val.CompareTo(that.val);
    }

您需要:

public int CompareTo(Point that)
    {
        if(this.val == that.val) {
            if(this.x == that.x) {
                return this.y.CompareTo(that.y);
            }
            else {
                return this.x.CompareTo(that.x);
            }
        }
        return val.CompareTo(that.val);
    }