c# - 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 中无法编译。
解决方案
推荐阅读
- php - 从站点目录的 url 下载文件
- git - 删除项目失败并显示错误消息。
- regex - 十六进制代码验证在 laravel 中不起作用
- java - 使用 jCo(3.x) 访问 SAP 表时未获取数据)
- javascript - 如何通过 JavaScript 代码获取 Firefox 浏览器的用户媒体权限(相机和麦克风)?
- javafx - 如何在折线图 javafx 中显示所需的 setTickLabelsVisible 数量
- javascript - '。' 大写字母之间没有以辅助功能的声音朗读
- reactjs - 用户登录页面后根据用户类型准备抽屉导航菜单
- django - 定期调用 django 函数
- javascript - 在没有互联网的情况下从安卓应用程序与 PHP 通信?