Smallest unused ID
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的更多相关文章
- Juno 版 Keystone 主配置文件 keystone.conf 详解
本文全面解读Icehouse发行版keystone的配置文件keystone.conf,由于从keystone提供的服务或依赖的基础设施角度入手,因此[DEFAULT]部分可能被拆分到很多子块中. 关 ...
- Android Lint Checks
Android Lint Checks Here are the current list of checks that lint performs as of Android Studio 2.3 ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
- Oracle Outline总结
一.基本概述 Oracle Outline,中文也称为存储大纲,是最早的基于提示来控制SQL运行计划的机制.也是9i以及之前版本号唯一能够用来稳定和控制SQL运行计划的工具. outline是一个hi ...
- Gym - 101102C线段树
Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from t ...
- Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算
D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...
- [转]Redis cluster failover
今天测试了redis cluster failover 功能,在切换过程中很快,但在failover时有force 与takeover 之分 [RHZYTEST_10:REDIS:6237:M ~] ...
- CF878D D. Magic Breeding bitset
D. Magic Breeding time limit per test 4 seconds memory limit per test 1024 megabytes input standard ...
- Performance Tuning Using Linux Process Management Commands
[root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...
随机推荐
- sql2005下载和安装
下载地址:个人百度网盘 http://pan.baidu.com/s/1kTvKIZd sql05安装包.rar 1.54G这个 安装方法: 先安装Tool 在安装Server http://bbs. ...
- Linux定时任务crontab每三秒执行一次shell
第一种方法:当然首先想到的是写一个触发的脚本,在触发脚本中使用死循环来解决此问题,如下: cat kick.sh #!/bin/bash while : ;do /home/somedir/scrip ...
- JavaScript笔记(一)
JavaScript组成 EcmaScript:核心部分 作为解释器.几乎没有兼容性问题 DOM:Document Object Model,操作HTML页面的入口.有些操作不兼容. BOM:Brow ...
- CorelDRAW 文件实用工具 CDRTools 2
随着 CorelDRAW 更新脚步越来越频繁,版本之间兼容性问题越来越突出,特别是跨版本之间打开会有很多问题,比如:文字跑位.透镜变向.位图出错.颜色改变,甚至会造成文件损坏.最好的办法就是哪一个版本 ...
- linux设备驱动模型(kobject与kset)
Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...
- 【转】perl ping检测功能脚本代码
我的第一个用于生产环境的perl脚本,虽然不是很优秀,但也迈出了扎实的一步 :)领导有任务,给一批IP列表,ping每一台机器,如果没有响应就发邮件通知,通知的邮件需要分开,不能通知一个列表,得一封一 ...
- 七、mysql索引选择
.myisam,bdb,innodb,memory 单表至少支持16个索引 .create index id_index on emp (id) 为emp表创建一个名为id_index的id字段的索引 ...
- iOS开发学习路线图
很多初学iOS开发的人会经常问:“我想学iOS应该从何入手呢?”.作为一个做了2年多各种iOS开发的程序员,只想写写自己的一些心得体会,好和体验与不好的体验.写的不好,请多包涵.希望能起到抛砖引玉的作 ...
- HelloWorld和数据绑定
HelloWorld和数据绑定 目录导读: AngularJS 系列 学习笔记 目录篇 前言: 好记性不如烂键盘,随笔就是随手笔记,希望以后有用. 本篇目录: 1. Hello World 2. An ...
- css3 简单界面动画
asdasdasdasda asdasdasdasda