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 ...
随机推荐
- mapper配置
一:查询 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC &q ...
- 【C#】数据库备份及还原的实现代码【转载】
[转载]http://www.codesky.net/article/200908/128600.html C#数据库备份及还原1.在用户的配置时,我们需要列出当前局域网内所有的数据库服务器,并且要列 ...
- H5 适配 动画animation js touch
图片预加载jquery插件 jquery.imgpreload var load_img = [];load_img.push('http://m.pubuzhixing.com/Images/vot ...
- js控制进度条到达100%跳转界面一
进度条一般在手机上用到的比较广泛,刚好最近的项目也是一直在做手机站,这个特效是手机端的一个界面,现在我把改成pc端了,进度条的快慢速度和样式可自行调节,改动也是很方便的,不多说,看代码: <st ...
- HTTP Header 详解【转】
原文出自:http://kb.cnblogs.com/page/92320/ HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用 ...
- phpcms v9修改栏目描述的多行文本为编辑器方法
phpcms v9在添加栏目的时候,栏目描述为多行文本,无法满足有图片,以及格式的修改调整,那么仿站网今天告诉大家如何将他改为编辑器,方法如下 找到phpcms/moudles/admin/templ ...
- SendMessage 窗口函数
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回. MSD ...
- 深入理解jsavascript的作用域
一. JavaScript声明提前 在JavaScript中如果不创建变量,直接去使用,则报错: console.log(xxoo); // 报错:Uncaught ReferenceError: x ...
- jsp多条件查询及查询结果在同一页面显示(原创)
第一步,建立main.jsp页面,使用frameset分上下两个框架,上部是query.jsp.下部是detail .detail显示的是showdetail.jsp的页面 <title> ...
- (转)linux性能优化总结
感谢博客http://sillycat.iteye.com提供的资料 linux性能检查(一)介绍和CPU 通常监控的子系统有: CPU Memory IO Network 应用类型 IO相关,处理大 ...