Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leetcode220 error: incomparable types: int and <null>

I received an error when compiled the code below, which is "incomparable types: int and " on line 10.

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

    TreeSet<Integer> set = new TreeSet<>();
    for (int i = 0; i < nums.length; i++) {
        int floor = set.floor(nums[i] + t);//return largest number smaller than nums[i] + t or null
        int ceil = set.ceiling(nums[i] - t);//return least number larger than num[i] - t or null

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

        set.add(nums[i]);

        if (set.size() > k) {
            set.remove(nums[i - k]);
        }
    }

    return false;
}}

but if I add final keyword before treeset, floor and ceil, the code will be compiled successfully. Could anyone help me explain what's going on? Thanks.

...
final TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
    final Integer floor = set.floor(nums[i] + t);//return largest number smaller than nums[i] + t or null
    final Integer ceil = set.ceiling(nums[i] - t);//return least number larger than num[i] - t or null
...
like image 463
OhhhIrisHere Avatar asked Oct 15 '25 15:10

OhhhIrisHere


1 Answers

int floor defines floor as a basic int type. Integer floor defines floor as a non-trivial, class type. null is used only with non-trivial types

like image 57
Ripi2 Avatar answered Oct 17 '25 04:10

Ripi2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!