http://www.codewars.com/kata/smallest-unused-id

Description:

Hey awesome programmer!

You've got much data to manage and of course you use zero-based and non-negative ID's to make each data item unique!

Therefore you need a method, which returns the smallest unused ID for your next new data item...

Go on and code some pure awesomeness!

自己写了一段超级烂的

  Console.WriteLine("Length = {0}", ids.Length);
ids = ids.OrderBy(x => x).ToArray();
foreach (var number in ids)
{
Console.Write("{0} ", number);
}
Console.WriteLine(); int count = ids.Count();
int result = ids.Min() - ;
if (result != -)
{
return ;
}
bool loopAllNumber = true;
for (int i = ; i < count; i++)
{
result++;
if (result != ids[i])
{
loopAllNumber = false;
break;
}
}
Console.WriteLine("after for loop,result = {0}", result);
if (loopAllNumber)
{
result++;
}
return result;

其他人写的,使用了Linq的Except

   var max=ids.Max();
var excepts= Enumerable.Range(, max).Except(ids);
return excepts.Count() == ? max + : excepts.Min();

//
// 摘要:
// 通过使用默认的相等比较器对值进行比较生成两个序列的差集。
//
// 参数:
// first:
// 一个 System.Collections.Generic.IEnumerable<T>,将返回其不在 second 中的元素。
//
// second:
// 一个 System.Collections.Generic.IEnumerable<T>,如果它的元素也出现在第一个序列中,则将导致从返回的序列中移除这些元素。
//
// 类型参数:
// TSource:
// 输入序列中的元素的类型。
//
// 返回结果:
// 包含两个序列元素的差集的序列。
//
// 异常:
// System.ArgumentNullException:
// first 或 second 为 null。
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);

最新文章

  1. Cisco VPN can&#39;t work in Win8
  2. spring入门(五)【springMVC环境搭建】
  3. C#利用浏览按钮获得文件路径和文件夹路径
  4. stm32时钟分析
  5. 修改TrustedInstaller权限文件(无法删除文件)
  6. Android 中的AsyncTask
  7. Hibernate映射类型对照表
  8. ServletContentLIstener接口演示ServletContext的启动和初始化
  9. CCNA网络工程师学习进程(9)GNS3的安装与配置
  10. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting
  11. c#XML转换成实体
  12. 《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(下)
  13. 版本控制工具Git工具快速入门-Windows篇
  14. SQL Server 备份还原
  15. react router @4 和 vue路由 详解(五)react怎么通过路由传参
  16. Bootstrap之Bootstrap组件
  17. (转)python的ConfigParser模块
  18. CustomSqlSessionFactoryBean
  19. win2003无线网卡驱动无法安装解决方法
  20. Java虚拟机10:Client模式和Server模式的区别

热门文章

  1. 转载:邮箱正则表达式Comparing E-mail Address Validating Regular Expressions
  2. PHP中刷新输出缓冲,立即输出数据
  3. 常用mysql命令大全
  4. HttpUnit学习笔记
  5. C# 判断点是否在多边形内
  6. wpf 绑定ObservableCollection 的Count属性
  7. Oracle RAC集群安装之:Grid软件安装过程蓝屏
  8. C#设计模式学习资料--外观模式
  9. 解决mysql中表字符集gbk,列字符集Latin1,python查询乱码问题
  10. AirDrop显示名字的修改问题