简体   繁体   English

将null作为整数类型的默认值

[英]Get null as a default for integer types

An algorithm from Leetcode Discussion uses Binary Search Tree to hold a range of values from an input array to check if this array contains values that are different at most by t, and their indexes are at most k far from each other. Leetcode Discussion中的一种算法使用二进制搜索树来保存输入数组中的某个值范围,以检查该数组是否包含最多相差t的值,并且它们的索引相距最多k个。

JAVA : JAVA

public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
    if (nums == null || nums.length == 0 || k <= 0) {
        return false;
    }

    final TreeSet<Integer> values = new TreeSet<>();
    for (int ind = 0; ind < nums.length; ind++) {

        final Integer floor = values.floor(nums[ind] + t);
        final Integer ceil = values.ceiling(nums[ind] - t);

        if ((floor != null && floor >= nums[ind])
                || (ceil != null && ceil <= nums[ind])) {
            return true;
        }

        values.add(nums[ind]);
        if (ind >= k) {
            values.remove(nums[ind - k]);
        }
    }
    return false;
}

I am struggling to make it work in C#. 我正在努力使其在C#中工作。 This code doesn't work as long as LastOrDefault and FirstOrDefault methods return 0 as a default. 只要LastOrDefaultFirstOrDefault方法返回默认值0,此代码就无法工作。 How to work around to get null as a default? 如何变通以获取null作为默认值?

C# : C#

public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t)
{
    if (nums == null || nums.Length < 2 || k < 1) return false;

    SortedSet<long> set = new SortedSet<long>();

    for (int i = 0; i < nums.Length; i++)
    {
        long l = (long)nums[i];

        long floor = set.LastOrDefault(n => n <= l);
        long ceil = set.FirstOrDefault(n => n >= l);

        if ((l - floor <= t) || (ceil - l <= t))
            return true;

        set.Add(l);
        if (i >= k) set.Remove((long)nums[i - k]);
    }
    return false;
}

One way would be to declare your set with nullable longs 一种方法是用可为空的long声明您的集合

var set = new SortedSet<long?>();

Or you could just not use the FirstOrDefault and do something like: 或者,您可能无法使用FirstOrDefault并执行以下操作:

var greaterOrEqualToOne = set.Where(n => n >= 1);

long? ceil = greaterOrEqualToOne.Any() ? greaterOrEqualToOne.First() : null;

Yet another way, cast them to long? 还有另一种方法,将它们long? first: 第一:

long? ceil = set.Select(n => (long?)n).FirstOrDefault(n => n >= 1);

使用nullable long在LastOrDefault上获取null

SortedSet<long?> set = new SortedSet<long?>();

If you want to retrieve int/long/... values including null, then you need to user Nullable Types 如果要检索包含null的int / long / ...值,则需要使用Nullable Types

And in your source code will be: 在您的源代码中将是:

 SortedSet<long?> set = new SortedSet<long?>();

Other answers are technically correct (use nullable types on your set) 其他答案在技术上是正确的(在您的集合上使用可为空的类型)

SortedSet<long?> set = new SortedSet<long?>();

However, in order to ever receive null value from FirstOrDefault / LastOrDefault you need to pass the int?[] nums to your method, because you fill your set by casting 但是,为了从FirstOrDefault / LastOrDefault接收空值,您需要将int?[] nums传递给您的方法,因为您通过强制转换来填充集合

long l = (long)nums[i];

which won't get you null value from int array even when casted to nullable long? 即使将其强制转换为可为空的long? ,也不会从int数组中获取空​​值long? .

In other words - your method needs to get nullable array in order to work with null values. 换句话说-您的方法需要获取可为null的数组才能使用null值。

And if you won't ever be working with nulls on provided array why not just iterate from i = 1 ? 而且,如果您永远不会在提供的数组上使用null,为什么不仅仅从i = 1进行迭代呢?

You can cast int/long it to nullable type (?) as other users have stated and then use DefaultIfEmpty to create a default collection that contains null if the collection turns out to be empty. 您可以将int / long强制转换为可空类型(?),如其他用户所述,然后使用DefaultIfEmpty创建一个默认集合,如果该集合结果为空,则该集合包含null。

SortedSet<long?> set = new SortedSet<long?>();

long? floor = set.Where(n => n <= l).DefaultIfEmpty().LastOrDefault();
long? ceil = set.Where(n => n >= l).DefaultIfEmpty().FirstOrDefault();

If you want to declare int,long... values as NULL, you need to declare like this: 如果要将int,long ...值声明为NULL,则需要这样声明:

int? empId;
if(empId is null)
{
    // do this
}
else
{
    //do this
}

In the same way you have to use: 同样,您必须使用:

SortedSet<long?> sortedSet = new SortedSet<long?>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM