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);

Smallest unused ID的更多相关文章

  1. Juno 版 Keystone 主配置文件 keystone.conf 详解

    本文全面解读Icehouse发行版keystone的配置文件keystone.conf,由于从keystone提供的服务或依赖的基础设施角度入手,因此[DEFAULT]部分可能被拆分到很多子块中. 关 ...

  2. Android Lint Checks

    Android Lint Checks Here are the current list of checks that lint performs as of Android Studio 2.3 ...

  3. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

  4. Oracle Outline总结

    一.基本概述 Oracle Outline,中文也称为存储大纲,是最早的基于提示来控制SQL运行计划的机制.也是9i以及之前版本号唯一能够用来稳定和控制SQL运行计划的工具. outline是一个hi ...

  5. Gym - 101102C线段树

    Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from t ...

  6. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  7. [转]Redis cluster failover

    今天测试了redis cluster  failover 功能,在切换过程中很快,但在failover时有force 与takeover 之分 [RHZYTEST_10:REDIS:6237:M ~] ...

  8. CF878D D. Magic Breeding bitset

    D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...

  9. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

随机推荐

  1. 【原】Oracle拼接字段

    select FLIGHT_DATE, replace(wm_concat(FLIGHT_NO), ',', '*') FLIGHT_NO from T2001 group by FLIGHT_DAT ...

  2. js 闭包 详解

    闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 闭包的特性 闭包有三个特性: 1.函数嵌套函数 2.函数内部可以引用外部的参数和变量 3.参数 ...

  3. php 批量生成html,txt文件的方法(实例代码)

    php批量生成html,txt文件的实现代码. 首先,建立一个conn.php 链接数据库. <?php $link = mysql_connect("mysql_host" ...

  4. oracle linux了解基本命令行

    1.      Linux的版本:核心(kernel)版本和发行(distribution)版本 2.      复制.删除和移动文件的命令 cp [选项] 源文件或目录  目标文件或目录 -R,-r ...

  5. W25Q32的使用

    一.W25Q32简介 W25Q32是华邦公司推出的大容量“SPI  FLASH” 产品. 1.容量 32M-Bit/4M-byte(4,194,304) 2.存储结构 页:256-bytes 扇区:4 ...

  6. 关于搭建Android环境的时候遇到 'could not find adb.exe!'的问题

    关于'could not find adb.exe'的问题 问题原因: 文件所处位置和Android_home变量指路径不一致 文件路径: 解决方法: 直接将相关文件退拽至变量值的路径下即可 小结:a ...

  7. doc下批处理文件的感想

    这段时间忙着为我们的爬虫程序做一个守护进程,想来想去还是用脚本比较好,所以用了点时间仔细的研究了一下,这里有一点点经验想分享给大家,也不能说是经验了,只能说是我写这个的时候所用到的知识: 1.task ...

  8. NSUserDefault 的使用(好东东,留着)

    1.NSUserDefault的使用: 作用:NSUserDefaults类提供了一个与默认系统进行交互的编程接口.NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等 ...

  9. 1588: [HNOI2002]营业额统计 - BZOJ

    Description营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  10. 1060: [ZJOI2007]时态同步 - BZOJ

    Description小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的 ...