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 ...
随机推荐
- artdialog 提示 确定或取消
dialog({ title:'提示', content:"下载需扣除" + point + "个积分<br />重复下载不扣积分,需要继续吗?", ...
- [原创] linux课堂-学习笔记-课程3.Linux目录结构介绍及内核与shell分析
一.目录说明 1.1 bin 一般用户,可执行的系统内置命令 1.2 sbin 系统管理员,可执行的系统内置命令 1.3 boot 启动文件目录,启动有关的文件都保存在此 1.4 dev 设备管理文件 ...
- php+mysql非缓冲查询(如何循环大数组)
另外一种PHP查询模式是非缓冲查询,数据库服务器会一条一条的返回数据,而不是一次全部返回,这样的结果就是PHP程序消耗较少的内存,但却增加了数据库服务器的压力,因为数据库会一直等待PHP来取数据,一直 ...
- php入门变量之字符串
字符串只是一块用引号括起来的字符:字母.数字.空格.标点符号,等等. 下面列出的全都是字符串: 'Huige' "In watermelon sugar" '100' 'Augus ...
- centos 安装ecshop出现date错误
centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...
- vector 内部方法大全 学习(初学者的参考资料)
1 vector构造函数:也就是如何对一个vector对象进行初始化 ////////////////////////////代码//////////////////////////////// ...
- Python操作RabbitMQ初体验(一)
由于想用Python实现一套分布式系统,来管理和监控CDN的内容与运行状态,误打误撞认识了RabbitMQ,推荐的人很多,如余锋<我为什么要选择RabbitMQ>等等. 在MQ这个词汇映入 ...
- 拥抱ARM妹子 序章!ARM妹子~~ 哥我来啦!
一个负心汉即将移情别恋,从51转到ARM妹子啦?其实8是的,俺准备开后宫.哇——咔~咔~~.考虑功耗和成本等问题,只有51肯定是不够的,所以嘛~~(一脸坏笑)嘿嘿~~,ARM妹子俺追定了.出于对ARM ...
- 关于postgresql——常用操作指令
创建数据库 CREATE DATABASE test WITH OWNER = postgres ENCODING = 'UTF8'; 进入控制台方法,在postgreSQL的安装目的bin下执行命令 ...
- 由于本公司项目需要,现急需拥有微软MCSE证书的人才,一经录用,待遇从优!
志鸿科技于1988年在香港创办,从事资讯科技服务,为本地及跨国金融企业提供各种合适的企业应用软件及方案,并于2000年6月30日在香港联合交易所创业板成功上市 (股票代号8048),香港长江实业.新加 ...