首页 > 解决方案 > Codility 不编译包含 HashSet 和 lambda 的 C# 和 VB 代码

问题描述

我今天尝试 Codility 时遇到了一个编译器问题,它不允许我使用哈希集或 lambda。

这是我被要求执行的任务:

写一个函数:

类解决方案{公共int解决方案(int [] A);}

即,给定一个包含 N 个整数的数组 A,返回 A 中未出现的最小正整数(大于 0)。

例如,给定 A = [1, 3, 6, 4, 1, 2],函数应该返回 5。

给定 A = [1, 2, 3],函数应该返回 4。

给定 A = [−1, −3],该函数应返回 1。

为以下假设编写一个有效的算法:

N 是 [1..100,000] 范围内的整数;数组 A 的每个元素都是 [−1,000,000..1,000,000] 范围内的整数。

我在 VB 中的解决方案是:

Private Function solution(A As Integer()) As Integer
    Dim N As Integer = 1
    Dim hash As HashSet(Of Integer) = New HashSet(Of Integer)(A).Where(Function(x) x > 0).ToHashSet
    While hash.Contains(N)
        N += 1
    End While
    Return N
End Function

该解决方案在 Codility 中不起作用,它给了我错误“VBNC90019”。我用谷歌搜索,发现 lambda 在旧版本的 VB 中无法编译。(Codility 过时了吗?)

然后我决定在 C# 中尝试相同的方法:

using System;
using System.Linq;
using System.Collections.Generic;

class Solution {
    public int solution(int[] A) {
        int N = 1;
        HashSet<int> hash = new HashSet<int>(A).Where(x => x > 0).ToHashSet();
        while(hash.Contains(N))
        {
            N += 1;
        }
        return N;
    }
}

在这里,它告诉我它在 Collections 中找不到 HashSet..

我在这里做错了吗?它在 Visual Studio 中运行良好,但在 Codility 中无法编译。

标签: c#vb.netcompilation

解决方案


推荐阅读