今天开始刷刷codility上respectable的题目,难度适中。

https://codility.com/demo/take-sample-test/missing_integer

本题是找出数组里第一个非负的整数,要求复杂度O(n)。那么首先想到的做法是排序,但这样负责度就上去了。要从nlogn降到n,常见的一个做法是用hashtable,这里就可以用set记录。要注意的是全负的情况,所以这里用了maxVal=0作为初值。

#include <unordered_set>
using namespace std; int solution(vector<int> &A) {
// write your code in C++11
unordered_set<int> positives;
int maxVal = 0;
for (int i = 0; i < A.size(); i++)
{
if (A[i] > 0 && positives.find(A[i]) == positives.end())
{
positives.insert(A[i]);
if (A[i] > maxVal)
maxVal = A[i];
}
}
if (positives.size() == maxVal)
return maxVal+1;
for (int i = 1; i < maxVal; i++)
{
if (positives.find(i) == positives.end())
{
return i;
}
}
}

  

*[codility]MissingInteger的更多相关文章

  1. Codility经典算法题之九:MissingInteger

    Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  9. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...

  2. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  3. mac os使用homebrew来管理后台服务

    在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件. 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到 ...

  4. ActiveMQ之Queue

    Queue实现的是点到点模型,在以下的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue发送多个消息. 代码如下: public class QueueTest { /** * @ ...

  5. nodejs mongodb

    27017 nodejs指定vsisual studio版本 npm install mongodb --msvs_version=2013 npm install mongoose --msvs_v ...

  6. 登录超时自动退出,计算时间差-b

    // 此方法适用于所有被创建过的controller,且当前controller生命周期存在,如有错误的地方望大神斧正 //  说一下我们的需求和实现原理,需求:在点击home键退出但没有滑飞它,5分 ...

  7. html5上传文件并监听进度

    出处:   http://blog.csdn.net/small_rice_/article/details/21391625

  8. Kali Linux 优化过程

    修改输入法横向候选字 vim ~/.config/fcitx/conf fcitx-classic-ui.config 修改此行 为 false  :VerticalList=False   mb这玩 ...

  9. MVC3+AutoFac实现程序集级别的依赖注入

    1.介绍      所谓程序集级别的依赖注入是指接口和实现的依赖不使用配置文件或硬代码实现(builder.RegisterType<UserInfoService>().As<IU ...

  10. 移动端页面使用rem来做适配

    文/九彩拼盘(简书作者)原文链接:http://www.jianshu.com/p/eb05c775d3c6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. rem介绍 rem(font ...